-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (57 loc) · 1.91 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
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const methodOverride = require('method-override');
const Student = require('./models/student');
mongoose.connect('mongodb://localhost:27017/studentApp', {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => {
console.log("Mongo Connection successful");
})
.catch(err => {
console.log(err);
})
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: true}))
app.use(methodOverride('_method'))
app.get('/students', async (req, res) => {
const students = await Student.find({})
//console.log(students);
res.render('students/index', { students })
})
app.get('/students/new', (req, res) => {
res.render('students/new');
})
app.get('/students/:id/edit', async (req, res) => {
const {id} = req.params;
const student = await Student.findById(id);
res.render('students/edit', { student });
})
app.post('/students', async (req, res) => {
//console.log(req.body);
const newStudent = new Student(req.body);
await newStudent.save();
res.redirect('students');
})
app.get('/students/:id', async (req, res) => {
const {id} = req.params;
const student = await Student.findById(id);
//console.log(student);
res.render('students/details', { student })
})
app.put('/students/:id', async (req, res) => {
//console.log(req.body);
const {id} = req.params;
const student = await Student.findByIdAndUpdate(id, req.body, {runValidators: true, new: true})
res.redirect(`/students/${student._id}`);
})
app.delete('/students/:id', async (req, res) => {
//console.log(req.body);
const {id} = req.params;
await Student.findByIdAndDelete(id)
res.redirect('/students');
})
app.listen(3000, () => {
console.log("App is listening on port 3000");
})