-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (40 loc) · 1.21 KB
/
app.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
const express = require('express');
const app = express();
const path = require('path')
const userModel = require('./models/user');
app.set('view engine','ejs');
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname,'public')));
app.get('/', function(req,res){
res.render("index")
});
app.get("/read",async (req,res)=>{
let users = await userModel.find();
res.render("read",{users});
});
app.get("/delete/:id",async (req,res)=>{
let users = await userModel.findOneAndDelete({_id: req.params.id});
res.redirect("/read");
});
app.get("/edit/:userid",async (req,res)=>{
let user = await userModel.findOne({_id : req.params.userid});
res.render("edit",{user});
// res.send(user);
});
app.post("/update/:userid",async (req,res)=>{
let {image,email,name} = req.body;
let user = await userModel.findOneAndUpdate({_id : req.params.userid},{image,name,email},{new:true});
res.redirect("/read");
// res.send(user);
});
app.post("/create",async (req,res)=>{
let {name,email,image} = req.body;
let createdUser = await userModel.create({
name,
email,
image
});
res.redirect('/read');
});
app.listen(3000);