Using static(), staticCache(), and compress() node.js connect middleware

丶灬走出姿态 提交于 2019-12-20 09:55:37

问题


I have an Express 3.0 app and I'm trying to use the static(), staticCache(), and compress() middleware to serve and compress my static files. This is my current app.configure() function:

 app.configure(function() {
  app.use(express.favicon(__dirname + '/public/favicon.ico', {maxAge: 86400000}));
  app.use(express.bodyParser());
  app.use(express.cookieParser('foo'));
  app.set('views', __dirname + '/views');
  app.engine('.html', mustache({cache: true}).render);
  app.use(express.session({ store: sessionStore, secret: 'foo'}));
  app.use(express.staticCache());
  app.use(express.static(__dirname + '/public', {maxAge: 86400000}));
  app.use(express.compress());
});

// routes are loaded here

With this configuration, YSlow reports that my .css and .js files are not compressed and I can't get a cache hit without clearing my browser and refreshing the page multiple times. I also tried putting in a debug statement in the staticCache middleware to report cache hits and running ab -n 10000 -c 500 shows 0 cache hits.

Obviously I'm doing something wrong (I'm guessing the order or options are messed up) but I can't figure out what it is. Anybody have a working example with these three pieces of middleware working correctly together?


回答1:


  • start by putting the app.use(express.compress()); as the first middleware, remember the middleware live in a FIFO stack...
  • put the static part before the session parts, better yet, split them into separate routes (/app - with cookies, session and bodyParser, /static - with none)
  • ohh, and forget about staticCache it's deprecated and incompatible with static, if you want a much more mature static serving component use st


来源:https://stackoverflow.com/questions/10218003/using-static-staticcache-and-compress-node-js-connect-middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!