-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
114 lines (95 loc) · 3.42 KB
/
server.js
File metadata and controls
114 lines (95 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const express = require("express");
const app = express();
require("dotenv").config();
const mongoose = require("mongoose");
const moment = require("moment");
const path = require("path");
const methodOverride=require("method-override")
app.use(methodOverride("_method"))
// لأزم تحدد هنا الانجين اللي هيشتغل مع السيرفر
app.set("view engine", "ejs");
//هنا لازم تفهم ان ملف الس هو حاجه ستاتيك بالنسبه للسيرفر فلازم تعرفها
app.use(express.static("public"));
app.use(express.json());
// const bodyParser = require("body-parser");
app.use(express.urlencoded({ extended: true }));
const DB_CONNECTION = require("./config/connection");
const userRoutes = require("./routes/userRoutes");
const userModel = require("./models/userModel");
const errorHandler=require("./middleware/errorsHandle")
// start requests
app.get("/", async (req, res) => {
// طيب هنا عشان نعرف نعمل سيرفر ريندر لازم نستخدم فيو انجن
// بنستحدم ريندر مكان سيند
// ممكن تاخد البارامترز بتاعتنا اللي هنحتاج نستخدمها في الملف ككود جافاسكريبت
// هنستخدم مكتبه مومنت عشان نتحكم في شكل التاريخ
const findUsers = await userModel.find();
if (!findUsers) {
res.status(400);
throw new Error("Cannot find any user");
}
res.render("index", { arr: findUsers, moment: moment });
});
// التوضيح موجود في ملف الويب
app.get("/user/add.html", async (req, res) => {
res.status(200).render("user/add");
});
app.use("/user/add.html", require("./routes/userRoutes"));
//
app.get("/user/:id", async (req, res) => {
const findUser = await userModel.findById(req.params.id);
if (!findUser) {
res.status(400);
throw new Error("Cannot find this user");
}
console.log(findUser)
res.status(200).render("user/view",{obj:findUser,moment:moment});
});
//
app.get("/user/edit.html",()=>{
res.render("user/edit")
})
app.get("/edit/:id", async (req, res) => {
const findUser = await userModel.findById(req.params.id);
if (!findUser) {
res.status(400);
throw new Error("Cannot find this user");
}
console.log(findUser)
res.status(200).render("user/edit",{obj:findUser,moment:moment});
});
// app.use("/edit/:id",require("./routes/userRoutes"))
app.put("/edit/:id", async (req,res)=>{
const findUser = await userModel.findById(req.params.id);
if (!findUser) {
res.status(400);
throw new Error("Cannot find this user");
}
console.log(req.body)
const updateUser = await userModel.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.status(200).redirect("/")
})
app.delete("/delete/:id",async (req,res)=>{
const findUser = await userModel.findById(req.params.id);
if (!findUser) {
res.status(400);
throw new Error("Cannot find this user");
}
const deleteUser = await userModel.deleteOne({ _id: req.params.id });
res.status(200).redirect("/")
})
app.post("/search", async(req,res)=>{
const regex = new RegExp(`^${req.body.SearchTXT}`, 'i');
const findUser=await userModel.find({username:regex})
res.render("index", { arr: findUser, moment: moment });
})
app.use(errorHandler)
// DB connect
DB_CONNECTION();
// start server
const port = process.env.port || 3000;
app.listen(port, () => {
console.log(`app start listining on port ${[port]}`);
});