从 maxCDN 在 Node 应用程序中提供静态文件

Serving static files in Node app from maxCDN

目前我们有使用 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();
});

简单的方法是重定向:

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

这样的中间件