json-server
# 初始化
安装
npm i json-server
1创建文件
__json_server_mock__/db.json
{ "users": [] }
1
2
3修改
package.json
"scripts": { "json-server": "json-server __json_server_mock__/db.json --watch --post 3001 --middlewares ./__json_server_mock__/middleware.js" },
1
2
3执行(把服务器开着)
npm run json-server
1
# 定义数据
为
__json_server_mock__/db.json
初始化数据{ "users": [ { "id": 1, "name": "高修文" }, { "id": 2, "name": "高发放" }, { "id": 3, "name": "高好大" } ], "projects": [ { "id": 1, "name": "骑手管理", "personId": 1, "organization": "外卖组", "created": 3123123313 }, { "id": 2, "name": "鬼斧神工", "personId": 2, "organization": "让对方", "created": 3123433313 }, { "id": 3, "name": "夫人通过", "personId": 3, "organization": "应该会", "created": 3123123317 } ] }
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中间件
// Node.js 用Common JS 规范 module.exports = (req, res, next) => { if (req.method === "POST" && req.path === "/login") { if (req.body.username === "111" && req.body.password === "111111") { return res.status(200).json({ user: { token: "123", }, }); } else { return res.status(400).json({ message: "用户名或密码错误", }); } } next(); };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
上次更新: 2024/08/14, 04:14:33