节点套接字使用http挂断错误 [英] node socket hang up error with http

查看:123
本文介绍了节点套接字使用http挂断错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了节点http文档,将删除请求写入本地服务器,但收到套接字挂起错误,我检查过类似的问题:

I followed the node http document to write a delete request to the local server, but receive the socket hang up error, similar question I checked are:

错误:套接字使用节点v0挂起.12.0

但没有人真正适合我的情况.

but no one actually work for my scenario.

我认为这是代码错误,因为我使用邮递员为我工作,以下是我的代码

I believe it is the code error because I use postman it works for me, following is my code

var options = {
    hostname: 'localhost',
    port: 3000,
    path: '/accounts/abc'
    method: 'DELETE',
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
};

var order = {
    "secret": "abc_secret"
};

var content = JSON.stringify(order);
var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    res.on('data', function(chunk) {
        console.log('resp: ' + chunk);
    });
});


req.on('error', function(err) {
    console.error('error: ' , err.stack.split("\n"));
});

req.write(content);

req.end();

,错误是:

error:  [ 'Error: socket hang up',
  '    at createHangUpError (_http_client.js:215:15)',
  '    at TLSSocket.socketOnEnd (_http_client.js:300:23)',
  '    at TLSSocket.emit (events.js:129:20)',
  '    at _stream_readable.js:908:16',
  '    at process._tickCallback (node.js:355:11)' ]

推荐答案

显然,请求标头没有内容长度.因此,您在write函数中编写的任何内容都将被忽略.希望这是根本原因.

Apparently, the request header does not have content length. Therefore, whatever you wrote in the write function will be ignored. Hope this is the root cause.

这篇关于节点套接字使用http挂断错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆