如何调试 nodejs.重置:插座挂断



当我发送包含大量数据文件的表单时,套接字将被远程服务器突然关闭。这可能就是为什么我得到ECONNRESET错误的原因。如何解决?

我的 NodeJS 代码:

Promise((resolve, reject) => {
let sendOption = {
method: 'post',
host: host,
port: port,
path: path,
headers: form.getHeaders(),
timeout: options.maxTimeout ? 1 * 60 * 60 * 1000 : 2 * 60 * 1000,
}
if (options.userName && options.passWord) {
let auth = new Buffer(options.userName + ':' + options.passWord).toString('base64');
sendOption.Authorization = 'Basic ' + auth;
}
let request = http.request(sendOption, (res) => {
let body = ''
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', () => {
resolve(JSON.parse(body))
})
});
request.on('error', (err) => reject(err));
request.write(form.getBuffer());
request.end();
})

完全错误:

2020-02-17 19:09:25,570 ERROR 18664 [-/::1/-/14867ms POST /thirdPartUpload] nodejs.ECONNRESETError: socket hang up
at connResetException (internal/errors.js:570:14)
at Socket.socketOnEnd (_http_client.js:440:23)
at Socket.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1184:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
code: "ECONNRESET"
name: "ECONNRESETError"
pid: 18664
hostname: PC-HZ20139584
enter code here

POST 请求需要Content-Length:标头来声明其正文的长度。在您的情况下,您将 POST 的正文与request.write(form.getBuffer());一起发送

缺少或错误的Content-Length:标头可能会使服务器看起来像是试图利用漏洞。因此,服务器对这些请求关上门(突然关闭连接(,这些请求在您的客户端中显示为 ECONNRESET。

尝试这样的事情。

const buff = form.getBuffer();
request.setHeader('Content-Length', buff.length);    
request.write(buff);

最新更新