Server js:

var express = require('express'), compression = require('compression'), app = express(); app.use(compression()); app.use(express.static(rootDir)); 

In request: Accept-Encoding: gzip, deflate, sdch
The response is missing: Content-encoding: gzip

What could be the problem?

  • one
    And what file do you request? What data? - D-side

1 answer 1

When working with the compression module, you need to remember two subtle points:

  1. If the response body is less than the value specified in the threshold parameter, the response will not be compressed. This saves server resources in cases where compression does not give a significant gain to the client. By default, compression uses the value of 1kb .

  2. Not all files should be compressed. For example, applying compression to image/png files, in some cases, can increase their size. In the compression module, there is a filter parameter. The value of this parameter is a function that determines whether a response should be compressed. The default module is compressible , which allows compression only for text files (with text/* and application/json mime types).

Thus, if you really want to compress all server responses, you need to use this code:

 var express = require('express'), compression = require('compression'), app = express(); app.use(compression({ // Π‘ΠΆΠΈΠΌΠ°Π΅ΠΌ HTTP ΠΎΡ‚Π²Π΅Ρ‚Ρ‹, Ρ‚Π΅Π»ΠΎ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π΄Π»ΠΈΠ½Π½Π΅Π΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ Π±Π°ΠΉΡ‚Π° threshold: 1, // Π‘ΠΆΠΈΠΌΠ°Π΅ΠΌ HTTP ΠΎΡ‚Π²Π΅Ρ‚Ρ‹ нСзависимо ΠΎΡ‚ ΠΈΡ… mime-Ρ‚ΠΈΠΏΠ° filter: function() {return true;} })); app.use(express.static(rootDir));