使用static(),staticCache()和compress()node.js连接中间件 [英] Using static(), staticCache(), and compress() node.js connect middleware

查看:133
本文介绍了使用static(),staticCache()和compress()node.js连接中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Express 3.0应用程序,我试图使用 static() staticCache() ,和 compress()中间件来服务和压缩我的静态文件。这是我目前的 app.configure()函数:

  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 (。))
app.use(express.static(__ dirname +'/ public',{maxAge:86400000}));
app.use(express.compress());
});

//路由在这里加载

使用此配置,YSlow报告我的.css和.js文件没有被压缩,我不能在不清除浏览器并多次刷新页面的情况下获得缓存命中。我还尝试在staticCache中间件中添加一个调试声明来报告缓存命中并运行ab -n 10000 -c 500显示0个缓存命中。



显然我在做有什么问题(我猜这个命令或选项被搞砸了),但是我无法弄清楚它是什么。任何人都有一个工作示例,这三个中间件在一起正常工作?

解决方案


  • app.use(express.compress()); 作为第一个中间件,记住中间件生活在一个FIFO栈中...

  • 把会话部分之前的 static 部分更好地分割成不同的路由(/ app - with cookies,session和bodyParser,/ static - with none)

  • 哦,忘了 staticCache 它已被弃用,与 static 不兼容,如果您希望使用更加成熟的静态服务组件 st


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?

解决方案

  • 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

这篇关于使用static(),staticCache()和compress()node.js连接中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆