// package.json中
"scripts": {
...
"json_server": "json-server __json_server_mock__/db.json --watch --port 3006 --middlewares ./__json_server_mock__/middleware.js "
},
在创建__json_server_mock__ 下创建middleware.js
module.exports = (req, res, next) => {
if (req.method === "POST" && req.path === "/login") {
console.log("req::",req.body);
if (req.body.username === "liu" && req.body.password === "123456") {
return res.status(200).json({
user: {
token: '123'
}
})
} else {
return res.status(400).json({
message: "用户名密码错误"
})
}
}
}
post http://localhost:3006/login
Headers: Content-Type application/json
Body: { username: 'liu', password: '123456' }
请求即可