-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (128 loc) · 3.87 KB
/
index.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// express = require("express");
// app = express();
// port = 3000
// Student = require('./database.js')
// app.listen(port,()=>{
// console.log(`Running on ${port}`);
// });
// app.set("view engine","ejs");
// app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
// app.get('/',async(req,res)=>{
// students = await Student.find()
// res.render("index.ejs",{
// page:"CRUD with NodeJS and MongoDB - Home Page" ,
// title : "Dashboard",
// students:students});
// });
// app.post("/register",async(req,res)=>{
// const {name , mail , age} = req.body;
// newStudent = new Student({
// name , mail , age
// });
// studentsave = await newStudent.save();
// res.redirect("/");
// });
// app.get("/register",(req,res)=>{
// res.render("register")
// })
// app.get('/delete/:id',async(req,res)=>{
// // const {id}=req.params;
// // delelteStudent = await Student.findByIdAndDelete({_id:id});
// deleteStudent = await Student.findByIdAndDelete(req.params.id);
// res.redirect('/')
// })
// app.get('/edit/:id',async(req,res)=>{
// id = req.params.id;
// editStudent = await Student.findById({_id:id});
// if(editStudent == null){res.redirect('/')}
// else{res.render('edit'),({students:editStudent})}
// res.render('edit.ejs')
// })
// app.post("/update/:id",async(req,res)=>{
// id = req.params.id;
// const { name ,mail , age} = req.body;
// updateStudent = await Student.findByIdAndUpdate({_id:id},{
// name,mail,age
// },{new:true});
// res.redirect('/')
// })
const express = require("express");
const app = express();
const port = 3000;
const Student = require('./database.js');
app.listen(port, () => {
console.log(`Running on ${port}`);
});
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', async (req, res) => {
try {
const students = await Student.find();
res.render("index.ejs", {
page: "CRUD with NodeJS and MongoDB - Home Page",
title: "Dashboard",
students: students
});
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error879");
}
});
app.post("/register", async (req, res) => {
try {
const { name, mail, age } = req.body;
const newStudent = new Student({
name,
mail,
age
});
const savedStudent = await newStudent.save();
res.redirect("/");
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error0");
}
});
app.get("/register", (req, res) => {
res.render("register");
});
app.get('/delete/:id', async (req, res) => {
try {
const deleteStudent = await Student.findByIdAndDelete(req.params.id);
res.redirect('/');
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error1");
}
});
app.get('/update/:id', async (req, res) => {
try {
const id = req.params.id;
const editStudent = await Student.findById({_id:id});
if (editStudent == null) {
res.redirect('/');
} else {
res.render('edit.ejs', { students: editStudent });
}
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error2");
}
});
app.post("/update/:id", async (req, res) => {
try {
const id = req.params.id;
const { name, mail, age } = req.body;
const updateStudent = await Student.findByIdAndUpdate({_id:
id},
{ name, mail, age },
{ new: true }
);
res.redirect('/');
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error3");
}
});