Causes and solutions of Httpclieint request error Econnreset for node. js

Source: Internet
Author: User

Background notes

Recently there is a scenario in the work item:

A file system server is implemented using the node. JS Express Framework, which has an API for uploading files to clients. The client uses node. JS's httpclient to invoke the server-side API to upload files.

The client does not have any problem when uploading the small file, httpclient request error when uploading the large file,

Google a lot of information, and finally looked at the relevant source node. JS finally know the cause of the problem and solutions.

Cause of the problem

The problem occurs because theHttpserver provided by node. JS defaults to a timeout of 2 minutes, and when a request is processed for more than 2 minutes, Httpserver automatically shuts down the requested socket, and the client receives Econnreset's error message . Refer to node. js for source code.

Here we use an example to verify this.

Server-side:

The server side uses the Express framework to register a GET method route handler function with the path "". In this route handler function, timeout processing is set by settimeout, and the request is timed out after 3 minutes.

Const EXPRESS = require (' Express '); const util= Require (' Util '); Const app=Express (); App.get ("/",function(req, res, next) {Util.log ("Received a request."); SetTimeout (function() {Res.setheader (' Transfer-encoding ', ' chunked '); Res.status (200); Util.log ("Timeout") Res.write ("Hello World");    Res.end (); }, 3 * 60 * 1000)});varServer = App.listen (3001,function() {Sutil.log ("Server listening at Port 3001 ...");});

Client:

The client requests the server-side interface by calling the Http.request method and prints the returned information.

Const HTTP = require (' http '); const util= Require (' Util ')varopt ={host:' localhost ', Port:3001, Method:' GET ',};varreq = Http.request (OPT,function(res) {Util.log (' STATUS: ', Res.statuscode); Res.setencoding (' UTF8 '); varResulttext = ' '; Res.on (' Data ', (chunk) ={Resulttext+=Chunk;    }); Res.on (' End ', () ={util.log (resulttext); });}); Req.on (' Error ', (e) = ={Util.log (e);}); Util.log ("Start request ...") Req.end ();

Start the server side first, and then start the client. The result of the request is as follows:

Server-side:

bbash-3.2$ node App.js  ANov +: Geneva: --Server listening at port3001......  ANov +: Geneva: A-Received a request.  ANov +: to: A-Timeout

Client:

 bash-3.2  $ node App.js  21 : 02 : 22 - start request ...  21 : 04 : 22 -{[error:socket Hang up] code:  
    econnreset  } 

From the above results can be seen, the client after the request has been waiting for 2 minutes, the error econnreset errors.

Resolution measures

Workaround: Call the server-side server.settimeout () method to set the server-side timeout to a larger size or close the timeout mechanism directly (set the time-out to 0 to close).

Just use the above code, the client does not change, the server calls the Server.settimeout () method at the end of the file, as shown below,

var function () {    Sutil.log ("Server listening at Port 3001 ...") );}); Server.settimeout (0)

Start the server side first, and then start the client, running the following results:

Server-side:

bash-3.2$ node app.js    :notoginseng:3001  ......                                      A : $- Received a request.                                                       A : £º- timeout 

Client:

bash-3.2$ node app.js         :notoginseng:-  start request ...                                                         A : £ º £º                                                            29 

From the above results are visible, the client can normally receive the server-side return results.

(done)

Causes and solutions of Httpclieint request error Econnreset for node. js

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.