http - node.js - after get request, script does not return to console -
here simple script
var http = require("http"); http.get( website, function(res) { console.log("does not return"); return; }); if website variable 'http://google.com' or 'http://facebook.com' script not return console. if website variable 'http://yahoo.com' or 'http://wikipedia.org' returns console. difference?
by "return console" i'm assuming mean node exits , drops @ shell prompt.
in fact, node eventually exit of domains listed. (you impatient.)
what seeing result of http keep-alives. default, node keeps tcp connection open after http request completes. makes subsequent requests same server faster. long tcp connection still open, node not exit.
eventually, either node or server close idle connection (and node exit). it's google , facebook allow idle connections live longer amounts of time yahoo , wikipedia.
if want script make request , exit completes, need disable http keep-alives. can disabling agent support.
http.get({ host:'google.com', port:80, path:'/', agent:false }, function(res) { ... }); only disable agent if need specific functionality. in normal, long-running app, disabling agent can cause many problems.
there some other approaches can take avoid keep-alives keeping node running.
Comments
Post a Comment