-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (73 loc) · 2.11 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
79
80
81
82
83
84
85
86
87
88
89
90
91
const jsonServer = require('json-server');
const server = jsonServer.create()
const router = jsonServer.router('src/app/api/db.json')
const middlewares = jsonServer.defaults()
const db = router.db;
const name = 'todos';
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares)
// Add custom routes before JSON Server router
server.get('/summary', (req, res) => {
const todos = db.get(name).value();
let list = [...todos];
let completed = req.query.completed || null;
if (completed !== null) {
completed = completed === 'true';
list = list.filter(val => val.completed === completed);
}
let page = req.query._page || 1;
let limit = req.query._limit || 9999;
page = typeof page === 'string' ? Number(page): page;
limit = typeof limit === 'string' ? Number(limit): limit;
let size = Math.ceil(list.length / limit);
size = size === 0 ? 1 : size;
const uncompletedCount = todos.filter(val => !val.completed).length;
const result = list.slice((page -1) * limit, page * limit);
res.header('X-Total-Count', todos.length);
res.jsonp({
todos: result,
pages: {
page,
limit,
size,
uncompletedCount,
categoryCount: list.length,
totalCount: todos.length
}
})
});
// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser)
server.use((req, res, next) => {
// Continue to JSON Server router
next()
})
// Set All
server.put('/all', (req, res) => {
const todosDB = db.get(name);
const todos = todosDB.value();
const completed = req.query.completed === 'true';
todos.forEach(item => {
todosDB.updateById(item.id, { completed }).write()
});
res.jsonp({
todos
});
});
// Remove
server.delete('/completed', (req, res) => {
const todosDB = db.get(name);
const todos = todosDB.value();
todos
.filter(item => item.completed)
.map(item => {
todosDB.removeById(item.id).write();
})
res.status(200).send();
});
// Use default router
server.use(router)
server.listen(4201, () => {
console.log('JSON Server is running')
})