response.write 的 Node Js 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6068820/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:11:59  来源:igfitidea点击:

Node Js problems with response.write

node.jsstreamhttp-1.1

提问by James

When I try to utilize http stream connection for some reason write does not flush until I call response.end()
I am taking the code straight from the demo and do not understand what my problem is.
When I curl to the server my headers are correct.

当我出于某种原因尝试使用 http 流连接时, write 直到我调用 response.end() 才会刷新我
直接从演示中获取代码并且不明白我的问题是什么。
当我卷曲到服务器时,我的标题是正确的。

HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked


var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.write('hello');
      res.write(':');
      setTimeout(function(){ 
          res.end('World\n')},
          2000);
    }).listen(1337, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:1337/');

Why is the server not sending the write data?

为什么服务器不发送写数据?

采纳答案by Geoff Chappell

I seems to be browser specific behavior -- firefox shows the data ("Hello:") immediately while chrome seems to buffer and wait until the response is ended. Note that chrome also shows the data immediately if you write more data at first (e.g. I wrote 1000 "Hello"s).

我似乎是浏览器特定的行为——firefox 立即显示数据(“你好:”),而 chrome 似乎缓冲并等待响应结束。请注意,如果您一开始写入更多数据(例如,我写了 1000 个“Hello”),chrome 也会立即显示数据。

回答by Dve

I think I understand what you mean...

我想我明白你的意思了...

From the node.js docs:

来自 node.js 文档:

The first time response.write() is called, it will send the buffered header information and the first body to the client. The second time response.write() is called, Node assumes you're going to be streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body.

第一次调用 response.write() 时,它会将缓冲的头信息和第一个正文发送给客户端。第二次调用 response.write() 时,Node 假设您要流式传输数据,并单独发送。也就是说,响应被缓冲到正文的第一个块。

http://nodejs.org/docs/v0.4.7/api/all.html#response.write

http://nodejs.org/docs/v0.4.7/api/all.html#response.write

(Nice port usage BTW :) )

(不错的端口使用 BTW :) )

回答by Andrey Sidorov

Try to check your code with telnet or nc. curl usually buffers last line

尝试使用 telnet 或 nc 检查您的代码。curl 通常缓冲最后一行

回答by Shuping

this is due to you missed a enclosing "}" in your code at position after res.end('World\n')and before the comma.

这是因为您在代码中res.end('World\n')逗号前后的位置错过了封闭的“}” 。

回答by J?rn Horstmann

After fixing the missing curly brace, your code is working for me from a browser. Curl from the commandline seems to wait for the full response but wireshark confirms that it does use chunked encoding and the response was split into 2 packages in both cases.

修复丢失的大括号后,您的代码在浏览器中对我有用。命令行中的 Curl 似乎在等待完整的响应,但 wireshark 确认它确实使用了分块编码,并且在这两种情况下,响应都被分成了 2 个包。

I assume that the curl output is line buffered and waiting for the newline after 'World' before it prints anything. You can confirm this by printing another newline after 'hello:'.

我假设 curl 输出是行缓冲的,并在打印任何内容之前等待 'World' 之后的换行符。您可以通过在 'hello:' 之后打印另一个换行符来确认这一点。