NodeJs 错误:连接 ETIMEDOUT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23575683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJs Error: connect ETIMEDOUT
提问by gumenimeda
I run node myserver.jsthat contains the code bellow, and after 40-50sec I get the error(bellow the code). Why do I get an error when nothing is happening?
我运行node myserver.js包含以下代码的代码,40-50 秒后出现错误(代码下方)。为什么在什么都没发生时我会收到错误消息?
var options = {
host: 'google.com',
port: '80',
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 'post_data.length'
}
};
var subscribeRequest = require('http').request(options, function(res) {
console.log('send request');
}).on('error', function(err){console.log(err.stack)});
after 40-50sec I get this error:
40-50 秒后,我收到此错误:
Error: connect ETIMEDOUT
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
回答by mscdex
I see at least two things wrong here:
我在这里看到至少有两件事是错误的:
You're not ending your request. When you use
http.request()you have to call.end()on the request object returned when you are done sending any data so that the server knows there is no more data coming.http.get()automatically calls.end()for you because there are no bodies with GET requests.'Content-Length': 'post_data.length'should be'Content-Length': post_data.length
你没有结束你的请求。当您使用时,
http.request()您必须调用.end()完成发送任何数据后返回的请求对象,以便服务器知道没有更多数据到来。http.get()自动呼叫.end()您,因为没有带有 GET 请求的主体。'Content-Length': 'post_data.length'应该'Content-Length': post_data.length
回答by Adriano Valente
You have to call subscribeRequest.end()after declaring your request! If you don't do it, your request will never be sent
您必须subscribeRequest.end()在声明您的请求后致电!如果你不这样做,你的请求将永远不会被发送

