Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js http.ClientRequest错误_Node.js - Fatal编程技术网

Node.js http.ClientRequest错误

Node.js http.ClientRequest错误,node.js,Node.js,当一个请求被抛出到我的应用程序时,我向另一个Web服务器发送一个请求。但有时,我会收到一些我从未见过的奇怪的解析错误 下面是解析错误的样子: node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Parse Error at Socket.ondata (http.js:1150:24) at TCP.onread

当一个请求被抛出到我的应用程序时,我向另一个Web服务器发送一个请求。但有时,我会收到一些我从未见过的奇怪的解析错误

下面是解析错误的样子:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: Parse Error
    at Socket.ondata (http.js:1150:24)
    at TCP.onread (net.js:354:27)
这是我用来发送请求的代码:

var request = http.request(options);
request.end();
request.on('response', function (response) {
    response.on('end', function () {
        console.log('Response received');
    }
}

我的问题是如何减轻这种情况,以便在发生此错误时再次抛出请求?

首先,您应该在
.end()
方法之前将所有
.on()
侦听器放在
.end()
上,因为并非所有API
nextTick
它们的
.end()
方法

其次,如果您没有('error')侦听器,则节点的事件发射器会抛出错误。因此,您的代码应该更像这样

var request = http.request(options)
request.on('response', function (response) {
  response.on('end', function () {
    console.log('Response received.')
  })
  response.on('error', function (err) {
    console.log('I got a response error.')
    console.error(err.stack)
  })
})
request.on('error', function (err) {
  console.log('I got a request error.')
  console.error(err.stack)
})
request.end()

您是否已验证所连接的服务器是否返回正确的HTTP数据?可能尝试连接到
telnet
nc
并确认?是的。我的浏览器似乎也没有问题。在我发布这个问题后,我已经发现了“错误”侦听器,但我仍然不确定为什么会发生这种情况。有没有办法访问http.js并尝试调查问题?为什么会发生这种情况?发生了什么错误,或者为什么在没有侦听器的情况下抛出错误?我将调查
选项
,而不是
http
错误:发生解析错误
。与问题帖子中显示的错误相同。我认为
选项
是不相关的。