节点快速内容长度 [英] Node Express Content-Length

查看:142
本文介绍了节点快速内容长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用node.js并在一个小项目上表达。我设置响应头像吹:

I use node.js and express at a small project. I set response header like blow:

res.set({'Content-Type':'text/plain;charset=utf-8',    
'Content-Length': Buffer.byteLength(data, 'utf-8')});       

我可以使用 console.log 打印数据长度是317.

I can use console.log print data's length is 317.

但是在浏览器的控制台上,我只是得到这些:

But at browser's console, I just get these:


连接:保持活动

内容编码:gzip

内容类型:text / plain; charset = utf-8

日期:星期六,01 2013年6月08:21:59 GMT

转移编码:chunked

Vary:Accept-Encoding

X-Powered By:Express

Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/plain;charset=utf-8
Date:Sat, 01 Jun 2013 08:21:59 GMT
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:Express

所以,为什么 内容长度 消失了?

So, why the content-length disappeared?

推荐答案

响应有 Transfer-Encoding:chunked 。这里 Content-Length 不适用,因为内容是以响应正文内的一个或多个部分(块)发送的,标记表示每个个体的字节长度块。 http://en.wikipedia.org/wiki/Chunked_transfer_encoding

The response has Transfer-Encoding: chunked. Here Content-Length is not applicable, because the content is sent in one or more parts (chunks) inside the response body, with a marker indicating the byte-length of each individual chunk. http://en.wikipedia.org/wiki/Chunked_transfer_encoding

Node.js默认为 Transfer-Encoding:chunked 。但是,通过在本机http响应对象上设置 Content-Length 头来禁用此功能。 HTTP模块的文档说:

Node.js defaults to Transfer-Encoding: chunked. However, this is disabled by setting the Content-Length header on the native http response object. Documentation of HTTP module says:


发送Content-length头将禁用默认分组编码。

Sending a 'Content-length' header will disable the default chunked encoding.

code> Content-Encoding:gzip 在您的响应中,您可能已经启用了 connect.compress 中间件。 connect.compress 中间件删除 Content-Length 标题。

Going by the Content-Encoding:gzip header in your response, you probably have enabled the connect.compress middleware. The connect.compress middleware removes the Content-Length header.

无论如何,除非您自己生成gzip压缩的内容,否则您自己生成的 Content-Length 对于最终(gzipped)的响应正文不合适。幸运的是,连接中间件会为您处理。

In any case, unless you are generating gzipped content yourself, the Content-Length header you generate yourself would surely be inappropriate for the final (gzipped) response body. Luckily, the connect middleware takes care of that for you.

使用Express或Connect时,您不应该假定您使用res对象发送的内容实际上发送到客户端。之间有中间件。所有中间件都有能力改变响应的任何内容,包括更改响应正文,以及添加,删除和更改标题。

When using Express or Connect, you should not assume that the things you "send" with the res object actually get sent that way to the client. There's middleware in between. All middleware has the ability to change just about anything about the response, including changing the response body, and adding, removing and changing headers. Same goes for the request.

另请参阅以下问题:

  • Chunked encoding and content-length header
  • HTTP Chunked data size and content length

这篇关于节点快速内容长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆