https://github.com/typicode/json-server
Demo
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
| const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router(data);
const rewrite = jsonServer.rewriter({
"/api/*": "/$1",
});
const middlewares = jsonServer.defaults({
static: "static",
});
// rewrite response
router.render = (req, res) => {
res.locals.data = {
success: true,
data: res.locals.data,
total: res.get('x-total-count'),
};
res.jsonp(res.locals.data);
};
server.use(middlewares);
server.use(rewrite);
server.use(jsonServer.bodyParser);
// rewrite request
server.use((req, res, next) => {
if (req.query.from) {
req.query._start = req.query.from
}
if (req.query.limit) {
req.query._limit = req.query.limit
}
if (req.query.ids) {
req.query.id = req.query.ids.split(',')
}
next();
});
server.use(router);
server.listen(3000, () => {
console.log("JSON Server is running");
});
|
Relationship
_embed
1
2
3
4
5
6
| {
commentId: 1,
comment: 'A comment',
postId: 1,
// ...
}
|
/posts/1?_embed=comments
1
2
3
4
5
| {
postId: 1,
content: 'A post',
comments: [/* all comments postId=1 */]
}
|
_expand
1
2
3
4
5
| {
postId: 1,
userId:1
// ...
}
|
userId => user
/posts/1?_expand=user
1
2
3
4
5
6
7
8
9
| {
postId: 1,
userId:1,
user: {
id: 1,
// ...
}
// ...
}
|
Rewriter
1
2
3
4
5
| const rewrite = jsonServer.rewriter({
"/api/*": "/$1",
"/mock-api/*": "/$1",
"/post/v1/detail?id=([0-9]+):qs": "/post/$1?:qs",
});
|
https://github.com/kapouer/express-urlrewrite
https://github.com/pillarjs/path-to-regexp
HTTPS
<chrome://flags/#allow-insecure-localhost>
Refs
https://github.com/marak/Faker.js/
https://github.com/chimurai/http-proxy-middleware