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);
Compression does not work unless the client sends a "Accept-Encoding:gzip" request header. You can test the compression here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With