admin管理员组文章数量:1336586
well,
i'm pletely new to node.js. Starting to try it, i'm following the introduction made by Ryan Dahl () and at this point (around 0:17:00) there's an explanation about how server handles responses,
The basic example is to have a 'hello' output from webserver and then after 2 secs it es the 'world', this code is supposed to do that
//Require the webserver library
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'content-type' : 'text-plain' });
res.write('Hello\n');
//Asynchronous behavior
setTimeout(function() {
res.end('World\n');
}, 2000);
});
server.listen(3000);
So i run it, and i get the Hello World but there's only one response from server with the plete result, that is, request > 2 sec > 'Hello World'. Instead of request > Hello > 2 secs > World.
Why is that?, How can i change this behaviour?
I'm using v0.8.18,
curl -i http://localhost:3000
returns the right headers...
HTTP/1.1 200 OK
content-type: text-plain
Date: Sat, 26 Jan 2013 18:10:05 GMT
Connection: keep-alive
Transfer-Encoding: chunked
well,
i'm pletely new to node.js. Starting to try it, i'm following the introduction made by Ryan Dahl (http://www.youtube./watch?v=jo_B4LTHi3I) and at this point (around 0:17:00) there's an explanation about how server handles responses,
The basic example is to have a 'hello' output from webserver and then after 2 secs it es the 'world', this code is supposed to do that
//Require the webserver library
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'content-type' : 'text-plain' });
res.write('Hello\n');
//Asynchronous behavior
setTimeout(function() {
res.end('World\n');
}, 2000);
});
server.listen(3000);
So i run it, and i get the Hello World but there's only one response from server with the plete result, that is, request > 2 sec > 'Hello World'. Instead of request > Hello > 2 secs > World.
Why is that?, How can i change this behaviour?
I'm using v0.8.18,
curl -i http://localhost:3000
returns the right headers...
HTTP/1.1 200 OK
content-type: text-plain
Date: Sat, 26 Jan 2013 18:10:05 GMT
Connection: keep-alive
Transfer-Encoding: chunked
- That is the idea of HTTP, the request gets sent if it is pleted, which is after the 'World' is appended to the request, so after 2 seconds. – 11684 Commented Jan 26, 2013 at 18:51
- I see... but that's what's not explained about how node is supposed to work. In fact you can see in the demonstration that the response is received in two parts. – Alexander Fradiani Commented Jan 26, 2013 at 18:56
- people run into problems doing this with http across all kinds of programming langs, web servers, and clients. Something will buffer the output a tiny bit and destroy the progressive effect. Try padding the snot out of your strings with spaces, and try a different client. – goat Commented Jan 26, 2013 at 19:01
- With curl, your code performs as expected. A browser waits for the whole body, but curl prints "hello", waits 2 seconds then prints "worls". I copied your exact code and it's ok. – randunel Commented Jan 26, 2013 at 19:04
- @randunel you're right. I suppose i can mark that as an answer if you change it from ment. – Alexander Fradiani Commented Jan 26, 2013 at 19:10
3 Answers
Reset to default 6It is the browser that buffers the ining data until some amount has been received, before starting to render. Your Node code does just as you expect, it will sent the first part of the response, then wait for 2 seconds, then send the second half.
If you want to observe this behavior, you can send a bunch of spaces to make the browser empty its buffer. If you add this after your initial write, you will see the browser render the first half of the request.
var str = '';
for (var i = 0; i < 2000; i++){
str += ' ';
}
res.write(str);
Obviously don't do this in real code, but it's good to demonstrate the behavior.
With curl, your code performs as expected. A browser waits for the whole body, but curl prints "hello", waits 2 seconds then prints "worls". I copied your exact code and it's ok.
you can change behavior using content type text/html
res.writeHead(200, { 'content-type' : 'text/html' });
the browser will act differently on different content-type. also using content-type text-html will make curl hold buffer and only print after buffer finished
i just test this on experiment, still i dont know why on browser and on curl doing like that
本文标签: javascriptnodejsi can39t reproduce progressive response from serverStack Overflow
版权声明:本文标题:javascript - node.js - i can't reproduce progressive response from server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742411098a2469801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论