-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (60 loc) · 1.58 KB
/
server.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 express = require('express')
const {db,Todos} = require('./db')
const app = express()
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use('/', express.static(__dirname + '/front'))
db.sync().then(() => {
app.listen(3000)
})
.catch((err) => {
console.error(err)
})
app.get('/todo',async(req,res) =>{
const todos = await Todos.findAll()
res.send(todos)
})
app.get('/todo/:id', async (req, res) => {
if(isNaN(Number(req.params.id))){
return res.status(404).send({error:'todo id must be ab integer'})
}
const todo = await Todos.findByPk(Number(req.params.id))
if(!todo){
return res.status(404).send({
error: 'No todo found with id = ' + req.params.id,
})
}
res.send(todo);
})
app.post('/add',async(req,res) => {
if(typeof req.body.task !=='string'){
return res.status(404).send({ error: 'Task name must be provided'})
}
const newTodo = await Todos.create({
task: req.body.task,
done: req.body.done,
due: req.body.due,
})
res.status(201).send({ success: 'New task added', data: newTodo })
})
app.put('/todos',async(req,res)=>{
Todos.update(
{ done: req.body.done },
{ where: { id: Number(req.body.id)} }
)
.success(result =>
handleResult(result)
)
.error(err =>
handleError(err)
)
res.status(201);
})
app.get('/remove', async(req,res) =>{
Todos.destroy({
where: {
done: true,
}
})
res.status(201).send({});
})