Giter Site home page Giter Site logo

Comments (10)

dougwilson avatar dougwilson commented on April 20, 2024

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.

dougwilson avatar dougwilson commented on April 20, 2024
$ 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.

rodrigoreis22 avatar rodrigoreis22 commented on April 20, 2024

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.

dougwilson avatar dougwilson commented on April 20, 2024

That message shows that there was no Content-Type header in the response, which is why this module wouldn't compress it.

from compression.

dougwilson avatar dougwilson commented on April 20, 2024

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.

dougwilson avatar dougwilson commented on April 20, 2024

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.

rodrigoreis22 avatar rodrigoreis22 commented on April 20, 2024

Yeah.. It's weird.. I'll investigate more. Thanks,

from compression.

SergeyKorochansky avatar SergeyKorochansky commented on April 20, 2024

@rodrigoreis22 @dougwilson I had a similar problem. Thanks for your help! 👍

from compression.

fkenji avatar fkenji commented on April 20, 2024

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.

dougwilson avatar dougwilson commented on April 20, 2024

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:

  1. Version of this module
  2. Version of Node.js
  3. Version of Express
  4. Complete app.js code I can copy and paste and run on my machine to reproduce the issue
  5. The exact request to make to the app.js call. Ideally just a cURL or wget command I can just copy-and-paste to the request is identical.

from compression.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.