-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
48 lines (37 loc) · 951 Bytes
/
router.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
const express = require('express');
const valid = require('validator');
const router = express.Router();
const data = require('./data');
const todos = [
{
task: 'do something-1'
},
{
task: 'do something-2'
},
{
task: 'do something-3'
}
]
router.get('/', (req, res) => {
res.sendfile('./public/index.html')
});
router.post('/api/todos', (req, res) => {
todos.push(req.body);
res.json(todos);
})
router.get('/api/todos', (req, res) => {
res.json(todos);
})
router.delete('/api/todos/:id', (req, res) => {
const {id} = req.params;
todos.splice(id, 1);
res.json(todos);
})
router.put('/api/todos/:id', (req, res) => {
const {id} = req.params;
const {task} = req.body;
todos[id] = {task};
res.json(todos);
})
module.exports = router;