Koa-compress不起作用 [英] Koa-compress is not working
问题描述
这是我嵌入koa-compress中间件的代码
This is my code for embedding koa-compress middleware
app.use(compress({
filter: function (content_type) {
return /text/i.test(content_type)
},
threshold: 1,
flush: require('zlib').Z_SYNC_FLUSH
}))
以下是我的回复发送代码
And following is my response sending code
ctx.body = 'Hello world'
ctx.compress = true
ctx.set('Content-Type', 'text/plain')
ctx.set('content-encoding', 'gzip')
当我通过CURL到达URL时,我得到一个简单的纯文本,上面写着"Hello World",但我相信我应该得到一个压缩字符串,因为CURL默认情况下不进行解压缩.当我在ChromeBrowser上点击相同的URL时,出现错误消息ERR_CONTENT_DECODING_FAILED
When I hit the URL through CURL I get a simple plain text saying "Hello World" but I believe I should have I got a compressed string because CURL doesn't do decompression by default. And when I hit the same URL on ChromeBrowser, I get the error saying ERR_CONTENT_DECODING_FAILED
当我将content-encoding设置为gzip时,koa-compress应该已经压缩了我的响应文本,但是以某种方式却没有这样做.也许我在犯一些错误,但我不知道是什么?
When I set content-encoding to gzip, koa-compress should have compressed my response text but it's somehow not doing so. Maybe I'm making some mistake but I don't know what?
推荐答案
我再次重新测试了整个过程,我意识到我的代码无法正常工作,因为我已经手动设置了content-encoding
,我不应该设置和设置它应该由压缩中间件本身设置.我在假设中犯了一个错误,那就是应始终压缩响应.但是,经过大量研究,我现在才意识到,压缩仅在客户端使用Accept-Encoding
发送标头时才有效.如果支持的Accept-Encoding为gzip
,则响应为deflate
时将以gzip压缩,响应应以deflate
形式进行压缩,如果未提及,则响应应为简单的纯数据.我在curl中收到纯文本,因为curl不会在默认请求中发送Accept-Encoding
,但是当我使用以下代码发送curl请求时
I just retested the whole process once again and I realised that my code was not working because I had manually set content-encoding
, which I should not have set and it should be set by compression middleware itself. I was making mistakes in assuming that response should always be compressed. But what I realised now, after lots of research is that compression works only when the client sends a header with Accept-Encoding
. If supported Accept-Encoding is gzip
, the response will be compressed in gzip if it's deflate
, the response shall be compressed in deflate
form, and if it's not mentioned response shall be simple plain data. I was receiving plain text in curl because curl does not send Accept-Encoding
in its default request, but when I sent curl request using following code
curl -H 'Accept-Encoding: gzip' -D - http://localhost:3000
然后我收到了压缩形式的回复.所以我的代码一直在工作.只是我不应该已设置
then I received the response in compressed form. So my code has been working for always. Just that I should NOT have set
ctx.set('content-encoding', 'gzip')
如果设置了content-encoding
,则模块将无法运行并产生错误
因此我们不必显式设置内容编码,它应该由压缩中间件根据请求的Accept-Encoding
设置.
If content-encoding
is set, the module will not run and produce error
so we don't have to explicitly set the content-encoding, it should be set by compression middleware depending on Accept-Encoding
of the request.
因此正确的响应代码应如下所示
So the correct code to response should be like following
ctx.body = 'Hello world'
ctx.compress = true
ctx.set('Content-Type', 'text/plain')
这篇关于Koa-compress不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!