NodeJs get Web content http.get The default request timeout is too long, the program should be stuck for a while.
Based on the online data, a method of acquiring Web content with time-out settings has been modified.
The primary method uses SetTimeout to time out,
The code is as follows:
//gets the specified Web page, returning the HTMLfunctiongeturlhtml (URL, fn_success, Fn_error, timeout) {timeout= Timeout | | 2000; vartimeevent; varreq = Http.get (URL,function(res) {varhtml = ' '; Res.setencoding (' Utf-8 '); Res.on (' Data ',function(d) {window.cleartimeout (timeevent); HTML+=D; }). On (' End ',function() {window.cleartimeout (timeevent); //var dom = JQ (HTML); returnfn_success (HTML); }). On (' Error ',function(Err) {window.cleartimeout (timeevent); Fn_error (ERR); }); }). On (' Error ',function(Err) {window.cleartimeout (timeevent); Fn_error (ERR); }); Timeevent= Window.settimeout (function() {Fn_error (' Time out! '); Req.abort (); }, timeout);}//Usegeturlhtml (' http://www.xxx.com ',function(HTML) {console.log (HTML);},function(Err) {Console.log (err);},1000);
NodeJs Http.get method Request time too long problem processing!