{"id":932,"date":"2014-12-02T07:57:35","date_gmt":"2014-12-02T07:57:35","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=932"},"modified":"2015-08-16T20:11:03","modified_gmt":"2015-08-16T20:11:03","slug":"nodejs-code-executes-differently-when-run-via-browser-and-when-run-via-curl-in-terminal","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/nodejs-code-executes-differently-when-run-via-browser-and-when-run-via-curl-in-terminal\/","title":{"rendered":"NodeJS code executes differently when run via browser and when run via curl in terminal"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[932]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/21880491\/nodejs-code-executes-differently-when-run-via-browser-and-when-run-via-curl-in-t\">quesiton<\/a> was:<\/p>\n<p>I have the following simple NodeJS code:<\/p>\n<pre class=\"lang:default decode:true\">var http = require(\"http\");\r\n\r\nhttp.createServer(function(request, response){\r\n    response.writeHead(200);\r\n    setTimeout(function(){\r\n        response.write(\"This printed 3 secs later, ait?\");\r\n        response.end();},3000);\r\n\r\n    response.write(\"This will be printed before.\\n\");}).listen(8080);<\/pre>\n<p>If I run the script with\u00a0<code>node scriptname.js<\/code>\u00a0and then access it via curl in terminal like this:<\/p>\n<pre class=\"lang:default decode:true\">curl http:\/\/localhost:8080<\/pre>\n<p>I get an output as expected, first it prints\u00a0<code>This will be printed before.<\/code>, then after 3 seconds it prints\u00a0<code>This printed 3 secs later, ait?<\/code>.<\/p>\n<p>However, when I open\u00a0<code>http:\/\/localhost:8080<\/code>\u00a0in my browser (newest versions of Chrome, Firefox) page loads for 3 seconds and it prints the text\u00a0<code>This will be printed before. This printed 3 secs later, ait?<\/code>\u00a0all at once. Why does this happen and how could I make the same behavior in the browser?<\/p>\n<p><strong>edit:<\/strong>\u00a0So, as Ken stated in his answer<\/p>\n<blockquote><p>&#8230;this is simply due to the behavior of the browser rendering engine to render the contents. The rendering engine cashes the contents until response.end();<\/p><\/blockquote>\n<p>and advised to go and check out Socket.IO, I came up with this working example which uses\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/expressjs.com\/\" rel=\"nofollow\">express<\/a>and\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/socket.io\/\" rel=\"nofollow\">Socket.IO<\/a>:<\/p>\n<pre class=\"lang:default decode:true\">\/\/timeoutTest.js\r\nvar express = require('express'),\r\n    app = express(),\r\n    server = require('http').createServer(app),\r\n    io = require('socket.io').listen(server);\r\n\r\nserver.listen(8080);\r\n\r\napp.use(express.static(__dirname +'\/'));\r\n\r\napp.get('\/',function(req, res){\r\n   res.sendfile(__dirname +'\/timeoutTest.html');});\r\n\r\nio.sockets.on('connection',function(client){\r\n   client.emit('msg',\"This prints right now on connection.\");\r\n\r\n   setTimeout(function(){\r\n      client.emit('msg',\"This prints out after 3 secs.\");},3000);});<\/pre>\n<pre class=\"lang:default decode:true \">\/\/timeoutTest.html\r\n&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;script src=\"\/socket.io\/socket.io.js\"&gt;&lt;\/script&gt;&lt;script src=\"jquery.js\"&gt;&lt;\/script&gt;&lt;script&gt;\r\n    $(document).ready(function(){var server = io.connect('http:\/\/localhost');\r\n        server.on('msg',function(data){\r\n            $('body').append(data +\"&lt;br\/&gt;\");});});&lt;\/script&gt;&lt;\/head&gt;&lt;body&gt;&lt;\/body&gt;&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"color: #000000;\">The answer, by <a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/users\/1028880\/ken-okabe\">Ken OKABE<\/a>,\u00a0was:<\/p>\n<blockquote>\n<p style=\"color: #000000;\">I think this is simply due to the behavior of the browser rendering engine to render the contents.\u00a0The rendering engine cashes the contents until\u00a0<code>response.end();<\/code>\u00a0arrives, then renders whole.<\/p>\n<p style=\"color: #000000;\">Basically, HTML contents in the browser are never updated automatically by incremental\u00a0<code>server push<\/code>such as\u00a0<code>response.write<\/code>.\u00a0You must\u00a0<code>pull<\/code>\u00a0data from the server to the browser client by Ajax and DHTML\/js technique.\u00a0curl in terminal is completely different story in terms of the mere output of the node server.<\/p>\n<p style=\"color: #000000;\">If you seek more interactive behavior between\u00a0<code>node<\/code>\u00a0server and browser client, if you want\u00a0<code>server push<\/code>\u00a0feature,\u00a0<code>websocket<\/code>\u00a0is the way to go, and also investigate\u00a0<code>node stream<\/code>\u00a0stuff.\u00a0<code>Socket.IO<\/code>\u00a0is famous, and for\u00a0<code>node stream<\/code>\u00a0<a style=\"color: #4a6b82;\" href=\"https:\/\/github.com\/substack\/stream-handbook\" rel=\"nofollow\">https:\/\/github.com\/substack\/stream-handbook<\/a>\u00a0should be interesting to you.<\/p>\n<p style=\"color: #000000;\">I personally do this on my own project:\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/kenokabe.github.io\/MarkdownLive\/\" rel=\"nofollow\">http:\/\/kenokabe.github.io\/MarkdownLive\/<\/a>.\u00a0I write stuff using Markdown and needed a streaming preview, so I created by myself. The preview screen is a Browser HTML page, and the HTML contents rendered and updated incrementally in streaming manner.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=932"}],"version-history":[{"count":5,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/932\/revisions"}],"predecessor-version":[{"id":1698,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/932\/revisions\/1698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}