7

I have tested my local web server with Google's PageSpeed chrome extension. One of the results was that my web server doesn't have compression enabled. I am using node js on the backend with express 4.x. I googled a bit to find a solution to compress data and I found https://github.com/expressjs/compression a middleware for express. I tried to use it like this:

    var app                 = express();
    var compression         = require('compression');
    var server              = http.createServer(app);
    server.listen(CONFIG.PORT);
    app.use(compression());

Even though I did this pageSpeed is still recommending to compress data. What am I doing wrong?

exilonX
  • 1,712
  • 3
  • 26
  • 50
  • What data did it recommend to compress? `compression` middleware only compresses response – Vsevolod Goloviznin Feb 09 '15 at 09:09
  • I have made some changes and I am putting compression as the first middleware to use and it is compressing all the data sent by the server for example /api/mongoModel but it is not compressing .js, .css and .html files. – exilonX Feb 09 '15 at 09:14
  • I don't think that it will compress static files. – Vsevolod Goloviznin Feb 09 '15 at 09:29
  • 1
    Checkout related questions: [1](http://stackoverflow.com/questions/25340612/how-to-create-a-simple-node-server-that-compresses-static-files-using-gzip) [2](http://stackoverflow.com/questions/6370478/express-gzip-static-content) – laggingreflex Feb 09 '15 at 09:47

2 Answers2

17
app.use(compression());
app.use(express.static(__dirname+'/public'));

Put JS, CSS, HTML files below the public directory and they will be compressed.

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
Mark
  • 171
  • 1
  • 3
-2

//use compression to speedup page load

app.use(compression());

app.use('app', function (req, res, next) {});