目前我们使用keystone + nunjucks创建了现有项目,静态文件的所有路径都显示为/stc/img/someimage.jpg
,因此我不想更改模板中的链接。是否可以通过maxCDN从节点服务器中的中间件提供服务?类似的东西:
app.use((req, res, next) => {
if (
req.path.slice(-5) === '.jpeg' ||
req.path.slice(-4) === '.jpg' ||
req.path.slice(-4) === '.svg' ||
req.path.slice(-4) === '.png' ||
req.path.slice(-4) === '.gif' ||
req.path.slice(-4) === '.css' ||
req.path.slice(-3) === '.js'
) {
req.path = `https://domain.cdn-ssl.com${req.path}`;
}
next();
});
答案 0 :(得分:1)
简单方法是重定向:
app.use((req, res, next) => {
if (
req.path.slice(-5) === '.jpeg' ||
req.path.slice(-4) === '.jpg' ||
req.path.slice(-4) === '.svg' ||
req.path.slice(-4) === '.png' ||
req.path.slice(-4) === '.gif' ||
req.path.slice(-4) === '.css' ||
req.path.slice(-3) === '.js'
) {
res.redirect( `https://domain.cdn-ssl.com${req.path}` );
} else {
next();
}
});
或者您可以使用express-http-proxy
- https://www.npmjs.com/package/express-http-proxy