|
|
|
|
|
'use strict';
|
|
|
|
|
|
const path = require('path');
|
|
|
const cachePage = require('../../config/cache');
|
|
|
|
|
|
module.exports = () => {
|
|
|
return (req, res, next) => {
|
|
|
if (req.get('X-Requested-With') === 'XMLHttpRequest') {
|
|
|
res.set('Cache-Control', 'no-cache');
|
|
|
}
|
|
|
|
|
|
function onRender() {
|
|
|
let route = req.route ? req.route.path : '';
|
|
|
let appPath = req.app.mountpath;
|
|
|
let key = path.join(appPath, route.toString()); // route may be a regexp
|
|
|
|
|
|
req.app.set('etag', false);
|
|
|
|
|
|
// 如果存在cache配置,并且业务代码中没有设置
|
|
|
if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') {
|
|
|
res.set({
|
|
|
'Cache-Control': 'max-age=' + cachePage[key]
|
|
|
});
|
|
|
res.removeHeader('Pragma');
|
|
|
res.removeHeader('Expires');
|
|
|
} else {
|
|
|
res.set({
|
|
|
'Cache-Control': 'no-cache',
|
|
|
Pragma: 'no-cache',
|
|
|
Expires: new Date(1900, 0, 1, 0, 0, 0, 0)
|
|
|
});
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
res.on('render', onRender);
|
|
|
next();
|
|
|
};
|
|
|
}; |
...
|
...
|
|