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);
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);
Share
Improve this question
asked Jan 19, 2016 at 8:40
JohnWickJohnWick
5,1499 gold badges40 silver badges79 bronze badges
1
- Edited my question, accidentally posted the version of server.js without the compression module. Fixed. – JohnWick Commented Jan 19, 2016 at 8:42
3 Answers
Reset to default 9Compression does not work unless the client sends a "Accept-Encoding:gzip" request header. You can test the compression here.
When turning compression on and off for testing make sure that you're doing a hard reload in chrome dev tools or otherwise you'll get a 'not-modified' response that is not compressed.
Your example code is working for me!
The compression middleware is a demanding horseman.
You need to set two things for it to work:
On the server, you need to specify a threshold size in bytes (default is 1024 (1kb) ) beyond which the response will be compressed. Start with 0:
app.use(compression({
threshold: 0
}))
On the client, you need to set a header called Accept-Encoding to 'gzip' .
GET /mimic HTTP/1.1
Accept-Encoding: gzip
Host: localhost:3005
To decompress a response from the client side, you can do sth like this:
const response = await axios.post(
'http://localhost:3005/mimic', { headers: { "Accept-Encoding": "gzip" }, responseType: 'arraybuffer' })
const xmlData = zlib.gunzipSync(response.data).toString()
In the above code, I am using the zlib and axios modules. zlib is a nodejs builtin module. There are other approaches if you're doing it from the browser.
Notice that response.data
is an array buffer.