Reputation: 1291
Currently we have existing project created with keystone + nunjucks, and all paths to static files looks like /stc/img/someimage.jpg
, so I don't want to change the links in template. Is it any way to serve them through middleware in node server from maxCDN? something like:
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();
});
Upvotes: 0
Views: 433
Reputation: 29172
Simple way is redirect:
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();
}
});
Or you can use middleware like express-http-proxy
- https://www.npmjs.com/package/express-http-proxy
Upvotes: 1