Comments (10)
Express runs everything in order; you need to app.use(compression({ threshold: 0 }));
above app.use(express.static(__dirname + '/dist'));
if you want express.static
stuff to be compressed.
from compression.
$ cat example.js
var express = require('express');
var app = express();
var compression = require('compression');
app.use(compression({ threshold: 0 }));
app.use(express.static(__dirname + '/dist'));
app.all("/*", function(req, res, next) {
res.sendFile('index.html', { root: __dirname + '/dist' });
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port)
});
$ DEBUG=compression node y
Example app listening at http://0.0.0.0:3000
compression gzip compression +0ms
$ curl -i -H'Accept-Encoding: gzip' http://127.0.0.1:3000/example.txt
HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Date: Wed, 04 Mar 2015 00:59:35 GMT
Cache-Control: public, max-age=0
Last-Modified: Wed, 04 Mar 2015 00:58:42 GMT
ETag: W/"3-395509304"
Content-Type: text/plain; charset=UTF-8
Vary: Accept-Encoding
Content-Encoding: gzip
Connection: keep-alive
Transfer-Encoding: chunked
▼ ♂╦╚T♦ :â╙A♥
from compression.
Ok, I changed it.
My request:
http://localhost:3000/styles/497f03bf.vendor.css
cap-ts-h5-ttys33:express-app rreis3$ DEBUG=compression node app.js
Example app listening at http://0.0.0.0:3000
compression undefined not compressible +0ms
compression no compression: filtered +1ms
When I try with a txt file, it works:
http://localhost:3000/example.txt
from compression.
That message shows that there was no Content-Type
header in the response, which is why this module wouldn't compress it.
from compression.
Simply adding a file 497f03bf.vendor.css
to the dist/
folder in your example works fine for me with your example code:
$ curl -i -H'Accept-Encoding: gzip' http://127.0.0.1:3000/497f03bf.vendor.css
HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Date: Wed, 04 Mar 2015 01:12:39 GMT
Cache-Control: public, max-age=0
Last-Modified: Wed, 04 Mar 2015 01:11:15 GMT
ETag: W/"0-1715649385"
Content-Type: text/css; charset=UTF-8
Vary: Accept-Encoding
Content-Encoding: gzip
Connection: keep-alive
Transfer-Encoding: chunked
▼ ♂♥
from compression.
There is always the possibility you have a corrupted node module somewhere. I would blow everything away and re-install:
$ rm -rf node_modules/
$ npm cache clean
$ npm install
and see if that helps.
from compression.
Yeah.. It's weird.. I'll investigate more. Thanks,
from compression.
@rodrigoreis22 @dougwilson I had a similar problem. Thanks for your help! 👍
from compression.
Had this happen to me as well with expressjs @4.13.3. Couldn't pinpoint exactly which express.js version where res.getHeader('Content-Type')
starts returning undefined
, as discussed on #37 . It might also be a conflict with other modules, I suppose.
For now the workaround could be implementing a filter
function that uses res.get('Content-Type')
instead of res.getHeader
, based of https://github.com/expressjs/compression/blob/master/index.js#L253 :
function shouldCompress(req, res) {
var type = res.get('Content-Type')
if (type === undefined || !compressible(type)) {
debug('%s not compressible', type)
return false
}
return true
}
app.use(compress({ filter: shouldCompress }));
from compression.
Hi @fkenji , we cannot use that API, as this module works with non-Express based servers. This is a generic middleware that works with the plain Node.js server. Also, I cannot replicate the issue using Express 4.13.3 (and this issue was originally opened before 4.13.3 even existed). So far every time this has happened, it turned out to be your modules/npm cache is just corrupted. Completely rm -rf node_modules && npm cache clean
and a re-install as resolved.
Rather than resurrecting this old issue, if you are still having this issue even after a complete re-install, please open a new issue with your issue and include the following information:
- Version of this module
- Version of Node.js
- Version of Express
- Complete app.js code I can copy and paste and run on my machine to reproduce the issue
- The exact request to make to the app.js call. Ideally just a
cURL
orwget
command I can just copy-and-paste to the request is identical.
from compression.
Related Issues (20)
- Setting Vary header although caching is disabled HOT 1
- "drain" event listener leak when using res.once("drain"); can't use res.removeListener("drain") HOT 3
- compresssion doesn't work ,the vue.txt is 2m HOT 2
- Content-Type: application/json; charset=utf-8 No effect HOT 2
- Question: Why this middleware HOT 2
- Corrupted compressed .js-files for Mac OS / Safari -clients HOT 11
- Is compression working when node server is running on a container? HOT 2
- middleware fails when the request has more than 1 values for accept-encoding header HOT 2
- Is compression result cached? HOT 1
- change Transfer-encoding HOT 1
- Why does the data size increase after compression HOT 1
- Force size to be a minimum... HOT 2
- Chunked encoding is broken after using this middleware HOT 1
- Using a current debug version HOT 1
- Deflate backwards HOT 7
- Compression instrumentation (before/after compression hooks) HOT 2
- Angular Not Compressing? HOT 2
- compression not working json payload HOT 6
- Crash when compressing characters like ū HOT 1
- Express returns a non-compliant HTTP/206 response when gzip is enabled HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compression.