2016-01-15 94 views
1

我写了一些代码使用模块cheerio,请求和longjohn昨天工作,但今天它总是抛出“ECONNREFUSED”错误。nodejs请求 - 总是返回econnresed

我尝试使用,使用简单的示例代码的请求:

var request = require('request'); 
request('http://www.google.com', function (error, response, body) { 
    if (!error && response.statusCode == 200) { 
     console.log(body) // Show the HTML for the Google homepage. 
    } 
    else{ 
     console.log(error); 
    } 
}) 

然而,它仍然返回:

{[Error: connect ECONNREFUSED] 
    code: 'ECONNREFUSED', 
    errno: 'ECONNREFUSED', 
    syscall: 'connect' } 

只是想知道这可能是这个原因,我可以连接到互联网罚款使用网络浏览器。

感谢

+1

可能是防火墙设置。 –

+0

感谢AMIR,我忘记了上次我运行此代码时我有一个代理(由于使用公司互联网,对开发人员有效) – ruby

回答

0

这可能是因为过程中不能有,因为防火墙或代理服务器的访问互联网。

检查您的防火墙和代理设置,并确保该过程可以访问互联网。

0

我的问题是我在启动服务器进程后立即执行get操作。我通过在setTimeout中包装get来解决错误

setTimeout(() => { 
    http.get('http://localhost:4003/state', (response) => { 
    console.log('response:', response) 
    response = response; 
    server.kill('SIGTERM'); 
    }).on('error', (err) => { 
    console.log('error:', err) 
    server.kill('SIGTERM'); 
    }); 
}, 1000); 
相关问题