2

I've the following scaffolded express application:

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'));
});

The only modification I've made to code generated by express generator:

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

The problem: processed to LESS files are gzipped and has X- HTTP-header with response time, but output from my controllers (HTML pages) is not gzippped and is served without headers.

Maybe I understand connect middleware wrong?

artvolk
  • 9,448
  • 11
  • 56
  • 85

2 Answers2

5

For the pages generated by your routes to be compressed (I assume that's what you mean by controllers) you need to move this line:

app.use(app.router);

after this line:

app.use(express.compress());

express.compress only affects those components added after it.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
1

For express 4, it is necessary to install the module.

var compress = require('compression')();
app.use(compress);
jabrena
  • 1,166
  • 3
  • 11
  • 25