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

别等时光非礼了梦想. 提交于 2019-12-13 14:36: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.



来源:https://stackoverflow.com/questions/28085546/sails-js-compression-doesn-t-seem-to-work-on-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!