我可能错了,但我无法在任何文档中找到它。 我正在尝试为任何响应全局设置内容类型,并且这样做:
// Set content type GLOBALLY for any response.
app.use(function (req, res, next) {
res.contentType('application/json');
next();
});
在定义我的路线之前。
// Users REST methods.
app.post('/api/v1/login', auth.willAuthenticateLocal, users.login);
app.get('/api/v1/logout', auth.isAuthenticated, users.logout);
app.get('/api/v1/users/:username', auth.isAuthenticated, users.get);
由于某种原因,这不起作用。你知道我做错了什么吗?分别在每种方法中设置它,可以工作,但我想要全局...
最佳答案
试试 this对于 Express 4.0:
// this middleware will be executed for every request to the app
app.use(function (req, res, next) {
res.header("Content-Type",'application/json');
next();
});
关于javascript - 如何在 node.js (express) 中全局设置内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671174/