Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS + ExpressJS Compression doing nothing?

I have read that I should be compressing requests to my Node server, so I used npm to install the compression module, added it with require() to my server.js, then passed it in as a function to app.use.

Then I looked at the network tab after, and I wanted to see how much the compression had saved me in kb. So I took the compression off, restarted my server, and it was the same amount of kb as with compression turned on?

Here is my server.js

var express = require('express'),
    app = express(),
    path = require('path'),
    apiRouter = require('./app/routes/api'),
    mongoose = require('mongoose'),
    compression = require('compression');

app.use(compression());
app.use(express.static('public'));
app.use('/api', apiRouter);

app.use('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/index.html'));
});

mongoose.connect('mongodb://localhost/triviaattack');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    //Connected to DB successfully.

});

app.listen(1337);
like image 706
JohnWick Avatar asked Dec 24 '22 10:12

JohnWick


1 Answers

Compression does not work unless the client sends a "Accept-Encoding:gzip" request header. You can test the compression here.

like image 147
Harsha Bhat Avatar answered Jan 06 '23 19:01

Harsha Bhat