[英]json-server add read-only route with middleware
在我的 json-server 设置中,我想设置一个只读路由。
因此,我没有在db.json
文件中设置该路由,而是使用了一个中间件(名为middleware.js
):
module.exports = (req, res, next) => {
if (req.url === '/authenticate') {
res.body = {
some_property: "some_value"
};
console.log("triggered!!!");
}
next();
};
所以我运行它给:
json-server --watch db.json --middlewares middleware.js
问题是,尽管触发了if
块并且我可以看到console.log
消息,但响应的正文将始终为空。
我使用以下方法解决了我的问题:
module.exports = (req, res, next) => {
if (req.url === '/authenticate') {
res.send({
some_property: "some_value"
});
} else {
next();
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.