2

我有以下脚手架快递应用程序:

var 
    express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , _ = require('underscore');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 5000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.compress());
  app.use(express.responseTime());
  app.use(require('less-middleware')({ src: __dirname + '/public' }));
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});

我对 express 生成器生成的代码所做的唯一修改:

  app.use(express.compress());
  app.use(express.responseTime());

问题:处理到 LESS 的文件被 gzip 压缩并且具有 X-HTTP-header 和响应时间,但是来自我的控制器(HTML 页面)的输出不是 gzipped 并且没有标题。

也许我理解连接中间件错误?

4

2 回答 2

5

对于要压缩的路由生成的页面(我假设这就是控制器的意思),您需要移动此行:

app.use(app.router);

在这一行之后:

app.use(express.compress());

express.compress只影响在它之后添加的那些组件。

于 2012-09-21T13:29:20.423 回答
1

对于express 4,需要安装模块。

var compress = require('compression')();
app.use(compress);
于 2015-07-11T10:59:52.650 回答