-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (76 loc) · 2.01 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
83
84
85
import express from 'express'
import { connectDataBase, getTodos, insertTodo, updateTodo, deleteTodo, deleteDone, deleteAll, showCompleted, pendingTodos } from './database.js'
import bodyParser from 'body-parser'
const app = express()
app.use(bodyParser.json()) // json middleware converting request to json
app.use(express.static('public')) // express middleware
connectDataBase()
app.get('/todo', async (req, res) => {
try {
const result = await getTodos()
res.json(result)
} catch (error) {
console.log(error.message)
res.send('Error 500 server not responding')
}
})
app.post('/addTodo', async (req, res) => {
try {
const addTodo = await insertTodo(req.body.todo)
res.json(addTodo)
} catch (error) {
console.log(error.message)
}
})
app.put('/updateTodo/:id/:property', async (req, res) => {
try {
const id = req.params.id
const property = req.params.property
const value = req.body.updateValue
const modifyTodo = await updateTodo(id, property, value)
res.json(modifyTodo)
} catch (error) {
console.log(error.message)
}
})
app.delete('/deleteTodo/:id', async (req, res) => {
try {
const id = req.params.id
const deleteEntry = await deleteTodo(id)
res.json(deleteEntry)
} catch (error) {
res.send(error.message)
}
})
app.delete('/deleteDone', async (req, res) => {
try {
const delDone = await deleteDone()
} catch (error) {
res.send(error.message)
}
})
app.delete('/deleteAll', async (req, res) => {
try {
const delAll = await deleteAll()
} catch (error) {
res.send(error.message)
}
})
app.get('/showCompleted', async (req, res) => {
try {
const Completed = await showCompleted()
res.json(Completed)
} catch (error) {
res.send(error.message)
}
})
app.get('/Pending', async (req, res) => {
try {
const Pending = await pendingTodos()
res.json(Pending)
} catch (error) {
res.send(error.message)
}
})
app.listen(3000, () => console.log(' its live on http://localhost:3000'))
// refer a restful doc