-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (38 loc) · 1.18 KB
/
app.js
File metadata and controls
48 lines (38 loc) · 1.18 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
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("/", (req, res) => {
res.render("index")
});
app.get("/read", async (req, res) => {
let users = await userModel.find();
res.render("read", {users});
});
app.get("/edit/:id", async (req, res) => {
let user = await userModel.findOne({_id: req.params.id});
res.render("edit", {user});
});
app.post("/update/:id", async (req, res) => {
let {name, email, image} = req.body;
let user = await userModel.findOneAndUpdate({_id: req.params.id}, {name, email, image},{new:true});
res.redirect("/read");
});
app.get('/delete/:id', async (req, res) => {
let deleteUser = await userModel.findOneAndDelete({_id: req.params.id});
res.redirect("/read");
})
app.post('/create', async (req, res)=>{
let {name, email, image} = req.body;
let createUser = await userModel.create({
name,
email,
image
});
res.redirect("/")
});
app.listen(3000)