Sails.js : compression doesn’t seem to work on jso

2019-07-07 17:44发布

问题:

I am trying to activate gzip compression on all JSON output on sails.js.

I added this in config/http.js:

order: [
   'startRequestTimer',
   'cookieParser',
   'session',
   'myRequestLogger',
   'bodyParser',
   'handleBodyParserError',
   'compress',
   'methodOverride',
   'poweredBy',
   '$custom',
   'router',
   'www',
   'favicon',
   '404',
   '500'
],
compress: require('compression')(),

I know the compress: require('compression')() line is called because I try with a wrong value and it crashes.

I restarted sails but the headers do not show gzip compression.

Requested headers show I accept gzip compression:

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate

Thank you for your help!

回答1:

I was struggling with the same thing, then I went through the Sails source code and found that the compress middleware is only activated if the app is run in production environment (i.e. NODE_ENV === production).

Could it be that you're doing this locally? I bet it will work if you set NODE_ENV to production.

This should at least apply to the default compress middleware, so maybe try removing the one you added yourself.