-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (58 loc) · 1.62 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
const dotenv = require("dotenv")
dotenv.config()
const express=require("express");
const app=express();
const User=require('./models/dataModel')
const path=require("path")
const PORT=process.env.PORT || 4000
require("./connection/bdConnection.js")
// connections print
const mongoose=require("mongoose");
const connection=mongoose.connection;
connection.once('open',(req,resp)=>{
console.log("connection seccessfull ")
})
// insert html file (insert.ejs) in browser
app.set('view engine','ejs');
app.use(express.urlencoded({extended:true}));
app.use(express.json())
app.use("/css",express.static(path.join(__dirname,'node_modules/bootstrap/dist/css')))
app.use("/js",express.static(path.join(__dirname,'node_modules/bootstrap/dist/js')))
//show data page
app.get("/",(res,resp)=>{
User.find({},(error,result)=>{
resp.render('show',{user:result})
})
});
// show insert page
app.get("/insert",(req,res)=>{
res.render("insert")
})
// post data
app.post("/addData",async(req,res)=>{
const newData= new User({
name:req.body.name,
gmail:req.body.gmail,
password:req.body.password
})
newData.save(()=>{
res.redirect("/")
})
})
// //show data using id in edit.ejs
app.get('/edit/:id', (req,res)=>{
User.findById(req.params.id,(error,result)=>{
res.render("edit",{user:result})
})
})
//update data
app.post('/update/:id',async (req,res)=>{
await User.findByIdAndUpdate(req.params.id,req.body)
res.redirect('/')
})
//delete data
app.get('/delete/:id',async (req,res)=>{
await User.findByIdAndDelete(req.params.id)
res.redirect('/')
})
app.listen(PORT);