NodeJs错误:连接ETIMEDOUT [英] NodeJs Error: connect ETIMEDOUT

查看:1379
本文介绍了NodeJs错误:连接ETIMEDOUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行包含以下代码的node myserver.js,并且在40-50秒后出现错误(下面的代码).什么都没发生时为什么会出现错误?

I run node myserver.js that 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?

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)});

40-50秒后,我收到此错误:

after 40-50sec I get this error:

Error: connect ETIMEDOUT
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

推荐答案

我在这里至少看到两点错误:

I see at least two things wrong here:

  1. 您不会结束您的请求.使用http.request()时,必须在完成发送任何数据后对返回的请求对象调用.end(),以便服务器知道不再有数据. http.get()为您自动调用.end(),因为没有带有GET请求的正文.

  1. 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'应该是'Content-Length': post_data.length

这篇关于NodeJs错误:连接ETIMEDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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