node.js Node Express 内容长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16870904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node Express Content-Length
提问by Allen Heavey
I use node.js and express at a small project. I set response header like blow:
我使用 node.js 并在一个小项目中表达。我像打击一样设置响应头:
res.set({'Content-Type':'text/plain;charset=utf-8',
'Content-Length': Buffer.byteLength(data, 'utf-8')});
I can use console.logprint data's length is 317.
我可以使用console.log打印数据的长度是 317。
But at browser's console, I just get these:
但在浏览器的控制台,我只得到这些:
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
连接:保持活动
内容编码:gzip
内容类型:text/plain;charset=utf-8
日期:星期六,2013 年 6 月 1 日 08:21:59 GMT
传输编码:分块
变化:接受编码
X-Powered-提供方:快递
So, why the content-lengthdisappeared?
那么,为什么内容长度消失了?
回答by Myrne Stol
The response has Transfer-Encoding: chunked. Here Content-Lengthis 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
回应有Transfer-Encoding: chunked。这里Content-Length不适用,因为内容是在响应正文中的一个或多个部分(块)中发送的,带有一个标记,指示每个单独块的字节长度。http://en.wikipedia.org/wiki/Chunked_transfer_encoding
Node.js defaults to Transfer-Encoding: chunked. However, this is disabled by setting the Content-Lengthheader on the native http response object. Documentation of HTTP modulesays:
Node.js 默认为Transfer-Encoding: chunked. 但是,这是通过Content-Length在本机 http 响应对象上设置标头来禁用的。HTTP 模块的文档说:
Sending a 'Content-length' header will disable the default chunked encoding.
发送“内容长度”标头将禁用默认的分块编码。
Going by the Content-Encoding:gzipheader in your response, you probably have enabled the connect.compressmiddleware. The connect.compressmiddleware removes the Content-Lengthheader.
根据Content-Encoding:gzip响应中的标头,您可能已启用connect.compress中间件。所述connect.compress中间件去除Content-Length报头。
In any case, unless you are generating gzipped content yourself, the Content-Lengthheader you generate yourself would surely be inappropriate for the final (gzipped) response body. Luckily, the connect middleware takes care of that for you.
在任何情况下,除非您自己生成 gzipped 内容,否则您自己Content-Length生成的标头肯定不适合最终(gzipped)响应正文。幸运的是,connect 中间件会为您处理这些问题。
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.
使用 Express 或 Connect 时,您不应假设您使用 res 对象“发送”的内容实际上是以这种方式发送给客户端的。中间有中间件。所有中间件都能够更改有关响应的任何内容,包括更改响应正文,以及添加、删除和更改标头。请求也是如此。
See also these questions:
另请参阅以下问题:

