简体   繁体   English

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

[英]Serving static files in Node app from maxCDN

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. 当前,我们已经使用keystone + nunjucks创建了现有项目,并且所有指向静态文件的路径都类似于/stc/img/someimage.jpg ,因此我不想更改模板中的链接。 Is it any way to serve them through middleware in node server from maxCDN? 是否可以通过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();
});

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 或者您可以使用express-http-proxy类的中间件express-http-proxy : //www.npmjs.com/package/express-http-proxy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM