【发布时间】:2015-10-18 03:09:58
【问题描述】:
我将 Express 用于 Web 服务,我需要将响应编码为 utf-8。
我知道我可以对每个响应执行以下操作:
response.setHeader('charset', 'utf-8');
是否有一种简洁的方法可以为 express 应用程序发送的所有响应设置标头或字符集?
【问题讨论】:
标签: node.js express http-headers
我将 Express 用于 Web 服务,我需要将响应编码为 utf-8。
我知道我可以对每个响应执行以下操作:
response.setHeader('charset', 'utf-8');
是否有一种简洁的方法可以为 express 应用程序发送的所有响应设置标头或字符集?
【问题讨论】:
标签: node.js express http-headers
只需使用对所有路由执行的中间件语句:
// a middleware with no mount path; gets executed for every request to the app
app.use(function(req, res, next) {
res.setHeader('charset', 'utf-8')
next();
});
并且,确保在您希望它应用到的任何路由之前注册它:
app.use(...);
app.get('/index.html', ...);
快递中间件documentation here.
【讨论】: