-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
86 lines (72 loc) · 2.38 KB
/
demo.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
const http = require('http');
const router = require('./router')();
const getAuthors = ({ filters, res }) => {
res.setHeader('Content-Type', 'application/json');
return JSON.stringify({
action: 'get all authors',
filters,
});
};
const getAuthorBooks = ({ filters, params, res }) => {
res.setHeader('Content-Type', 'application/json');
return JSON.stringify({
action: 'get all books by authorId',
params,
filters,
});
};
const getAuthorBook = ({ params, res }) => {
res.setHeader('Content-Type', 'application/json');
return JSON.stringify({
action: 'get book by authorId and bookId',
params,
});
};
const postAuthorBook = ({ params, body, res }) => {
res.setHeader('Content-Type', 'application/json');
return JSON.stringify({
action: 'post book by authorId',
params,
body,
});
};
const putAuthorBook = ({ params, body, res }) => {
res.setHeader('Content-Type', 'application/json');
return JSON.stringify({
action: 'put book by authorId and bookId',
params,
body,
});
};
const deleteAuthorBook = ({ params, res }) => {
res.setHeader('Content-Type', 'application/json');
return JSON.stringify({
action: 'delete book by authorId and bookId',
params,
});
};
// TEST WITH: curl -X GET "http://localhost:3001/authors?page=1&size=10"
router.handle('/authors', getAuthors).provideReqRes(true);
// TEST WITH: curl -X GET "http://localhost:3001/authors/1/books?page=1&size=10"
router.handle('/authors/:authorId/books', getAuthorBooks).provideReqRes(true);
// TEST WITH: curl -X GET "http://localhost:3001/authors/1/books/2"
router
.handle('/authors/:authorId/books/:bookId', getAuthorBook)
.provideReqRes(true);
// TEST WITH: curl -X POST -H "Content-Type: application/json" -d '{ "title": "test" }' http://localhost:3001/authors/1/books
router
.handle('/authors/:authorId/books', postAuthorBook)
.method('POST')
.provideReqRes(true);
// TEST WITH: curl -X PUT -H "Content-Type: application/json" -d '{ "title": "test" }' http://localhost:3001/authors/1/books/2
router
.handle('/authors/:authorId/books/:bookId', putAuthorBook)
.method('PUT')
.provideReqRes(true);
// TEST WITH: curl -X DELETE http://localhost:3001/authors/1/books/2
router
.handle('/authors/:authorId/books/:bookId', deleteAuthorBook)
.method('DELETE')
.provideReqRes(true);
const server = http.createServer(router.handler);
server.listen(3001);