-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
40 lines (34 loc) · 967 Bytes
/
routes.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
const express = require('express');
const router = express.Router();
const Student = require('./models/student');
router.get('/students',function(req,res){
Student.find({}).then(function(result){
res.send(result);
});
});
router.get('/students/:id',function(req,res){
Student.findOne({_id:req.params.id}).then(function(result){
res.send(result);
});
});
router.post('/students',function(req,res,next){
const {name,age} = req.body;
Student.create(req.body)
.then(function(result){
res.send(result);
})
.catch(next);
});
router.put('/students/:id',function(req,res){
Student.findOneAndUpdate({_id:req.params.id},req.body).then(function(result){
Student.findOne({_id:req.params.id}).then(function(student){
res.send(student);
})
});
});
router.delete('/students/:id',function(req,res){
Student.findOneAndRemove({_id:req.params.id}).then(function(result){
res.send(result);
});
});
module.exports = router;