-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (61 loc) · 2.08 KB
/
server.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
const express = require('express');
const User = require("./models/mongodb");
const bodyParser = require("body-parser");
const path = require('path');
const app = express();
const PORT = process.env.PORT || 8000;
var alert = false;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', express.static(path.join(__dirname, 'public')))
app.set('view engine', 'ejs');
app.get('/', async (req, res) => {
const userRecords = await User.find();
// console.log(data);
res.render('index', { alert: false, records: userRecords });
})
app.get('/addDetails', (req, res) => {
res.render('addDetails', { alert: false, title: "Add New User", record: {} });
})
app.get('/updateDetails/:id', (req, res) => {
var id = req.params.id
var title;
User.findById(id, (err, data) => {
// console.log(data)
if (err) throw err;
res.render("updateDetails", { title: "Update User", record: data, alert: false });
})
})
app.post("/", async (req, res) => {
const userRecords = await User.find();
try {
// res.send(req.body);
const userData = new User(req.body);
await userData.save();
res.status(201).render("addDetails", { alert: true, record: userRecords, title: "Add New User" });
} catch (error) {
console.log(error);
res.status(500).render('index', { alert: false, records: userRecords });
}
});
app.post("/updateDetails/:id", async (req, res) => {
console.log('user updating')
const id = req.params.id;
try {
const record = await User.findByIdAndUpdate(id, req.body)
res.status(201).render("updateDetails", { alert: true, record: {}, title: "Update User" });
} catch (e) {
console.log('some error occured' + e);
}
});
app.get('/delete/:id', (req, res) => {
User.findByIdAndRemove(req.params.id, (err, doc) => {
if (!err) {
res.redirect('/');
}
else { console.log('Error in employee delete :' + err); }
});
});
app.listen(PORT, () => {
console.log(`server is listening on PORT ${PORT}`);
})