Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js response.write的节点Js问题_Node.js_Stream_Http 1.1 - Fatal编程技术网

Node.js response.write的节点Js问题

Node.js response.write的节点Js问题,node.js,stream,http-1.1,Node.js,Stream,Http 1.1,当我出于某种原因尝试使用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) { r

当我出于某种原因尝试使用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/');

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

我想我理解你的意思

从node.js文档:

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


(很好的端口用法,顺便说一下:)

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


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

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

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

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

我刚刚注意到我的代码示例是错误的。这就是我正在使用的,它不是流式传输数据。它会一直等到调用end()。它确实是特定于平台的。您是否一直在运行它,或者有任何记录表明哪些浏览器平台将这些块排队?