-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
82 lines (55 loc) · 1.67 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
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
const express=require('express');
const mongoose=require('mongoose');
const bodyParser=require('body-parser')
const table=require('./models/StudList');
const app=express();
const dbURL="mongodb+srv://MjStar:[email protected]/nodefirst?retryWrites=true&w=majority"
mongoose.connect(dbURL)
.then((result)=>{app.listen(3000)})
.catch((err)=>{console.log(err)})
app.use(express.urlencoded({extended:true}));
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine','ejs');
app.get('/',(req,res)=>{
table.find().
then((result)=>{res.render('index',{student:result})})
.catch((err)=>{console.log(err)});
});
app.get('/numbers',(req,res)=>{
table.find().count((err,count)=>{
if(err) console.log(err);
else res.render('number',{count})});
});
app.get('/attendence',(req,res)=>{
res.render('attendence')
});
app.get('/:id',(req,res)=>{
const id=mongoose.Types.ObjectId(req.params.id);
table.findById(id)
.then((result)=>{
res.render('details',{StudDet:result})
})
.catch((err)=>{
res.status(404).render('404')
});
})
app.post('/',(req,res)=>{
console.log(req.body);
const Table=table(req.body);
Table.save()
.then((result)=>{res.redirect("/")})
.catch((error)=>{console.log(error)})
});
app.get('/delete/:id',(req,res)=>{
const id=mongoose.Types.ObjectId(req.params.id);
table.findByIdAndDelete(id)
.then((result)=>{
res.redirect('/')
})
.catch((err)=>{
console.log(err)
});
})
app.use((req,res)=>{
res.status(404).render('404')
})