After adding the line res.set('Content-Type', 'text/javascript')
Express is gzipping the response. The code is
module.exports = function(req, res, next) {
if (req.url === '/js/all.js') {
res.set('Content-Type', 'text/javascript');
res.end(cache);
} else {
next();
}
};
Now the headers of the response are:
X-Powered-By: Express
Vary: Accept-Encoding
Transfer-Encoding: chunked
Date: Wed, 12 Mar 2014 14:45:45 GMT
Content-Type: text/javascript
Content-Encoding: gzip
Connection: keep-alive
The reason for this is how the compress
middleware is designed. You can supply a filter
option to compress
:
app.use(express.compress({
filter : function(req, res) {
return /json|text|javascript/.test(res.getHeader('Content-Type'));
}
});
the compression is applied only to the requests that match the filter. The default filter of is:
function(req, res){
return /json|text|javascript|dart|image\/svg\+xml|application\/x-font-ttf|application\/vnd\.ms-opentype|application\/vnd\.ms-fontobject/.test(res.getHeader('Content-Type'));
};
If you do not supply a Content-Type
header, the request will not pass the filter and express will not gzip the response.