How to modify the nodejs request default timeout time?

前端 未结 6 1920
南旧
南旧 2020-11-27 17:05

I\'m using a Node/express server. The default timeout of express is 120,000 ms, but it is not enough for me. When my response reaches 120,000 ms, the console will log

6条回答
  •  时光取名叫无心
    2020-11-27 17:23

    With the latest NodeJS you can experiment with this monkey patch:

    const http = require("http");
    const originalOnSocket = http.ClientRequest.prototype.onSocket;
    require("http").ClientRequest.prototype.onSocket = function(socket) {
        const that = this;
        socket.setTimeout(this.timeout ? this.timeout : 3000);
        socket.on('timeout', function() {
            that.abort();
        });
        originalOnSocket.call(this, socket);
    };
    

提交回复
热议问题