Sails.js:压缩似乎不工作在json [英] Sails.js : compression doesn’t seem to work on json
问题描述
我试图在sails.js上的所有JSON输出上激活gzip压缩。
I am trying to activate gzip compression on all JSON output on sails.js.
我在config / http.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')(),
我知道 compress:require('compression')()
我重新启动了sails,但是标题不显示gzip压缩。
I restarted sails but the headers do not show gzip compression.
请求的标头显示我接受gzip压缩:
Requested headers show I accept gzip compression:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
感谢您的帮助!
推荐答案
我一直在努力做同样的事情,然后我经历了Sails 源代码,并发现压缩中间件仅在应用程序为在生产环境中运行(即NODE_ENV === production)。
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).
这是否可以在本地执行?我敢打赌,如果你将 NODE_ENV
设置为生产
,它会工作。
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.
这篇关于Sails.js:压缩似乎不工作在json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!