NodeJs Http.get method Request time too long problem processing!

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.