没有常见症状的套接字挂起错误

时间:2018-12-02 09:14:12

标签: javascript node.js sockets http request

我正在RPI3B上运行小型HTTP服务器。它记录了所发出的每个请求,因此在Postman中,我可以向服务器发出一个请求,它被接收,记录并返回正确的值。但是,使用此功能:

function get(action, path, content) {
    return new Promise(function(resolve, reject) {
        let body = "";
        let req = http.request({
            host: "10.0.0.12",
            method: content ? "post" : "get",
            path: `${action}?path=${path}`,
            headers: {
                token
            }
        }, res => {
            res.on('data', data => {
                body += data;
            })
            res.on("end", e => {
                resolve(body.toString());
            })
        });
        req.end(content);
        req.on("error", e => {
            reject(e);
        })
    });
}

我收到以下错误:

(node:24956) UnhandledPromiseRejectionWarning: Error: socket hang up
    at createHangUpError (_http_client.js:313:15)
    at Socket.socketOnEnd (_http_client.js:416:23)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1081:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:24956) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an asyn
c function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:24956) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not
 handled will terminate the Node.js process with a non-zero exit code.

我查看了此错误的常见症状,并注意到在发送请求后(与请求超时相反),我立即收到错误。我还注意到,我调用了req.end()方法来确保请求结束,但是结果保持不变。

对于任何可能引起这种情况以及如何解决它的见解,将不胜感激。

注意双方都是用Node.JS编写的。

0 个答案:

没有答案