优雅的方法是
res.set()在任何JSON输出之前简单地添加一个调用。在这里,您可以指定设置缓存控制标头,它将相应地进行缓存。
res.set('Cache-Control', 'public, max-age=31557600'); // one year另一种方法是简单地
res在路由中为JSON响应设置属性,然后使用后备中间件(错误处理之前)来呈现和发送JSON。
app.get('/something.json', function (req, res, next) { res.JSonResponse = { 'hello': 'world' }; next(); // important! });// ...// Before your error handling middleware:app.use(function (req, res, next) { if (! ('JSONResponse' in res) ) { return next(); } res.set('Cache-Control', 'public, max-age=31557600'); res.json(res.JSONResponse);})编辑:从改变
res.setHeader到
res.set的快递V4



