Node Js关于response.write的问题

发布于 2024-11-08 14:22:29 字数 599 浏览 7 评论 0原文

当我出于某种原因尝试利用 http 流连接时,写入不会刷新,直到我调用 响应.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/');

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

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/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨落画卷 2024-11-15 14:22:29

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

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).

○愚か者の日 2024-11-15 14:22:29

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

来自node.js 文档:

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

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

(顺便说一句,端口使用很好:))

I think I understand what you mean...

From the node.js docs:

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.

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

(Nice port usage BTW :) )

仙气飘飘 2024-11-15 14:22:29

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

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

源来凯始玺欢你 2024-11-15 14:22:29

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

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

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.

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:'.

审判长 2024-11-15 14:22:29

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文