1

我已经使用昨天工作的模块cheerio、request 和longjohn 编写了一些代码,但今天它总是抛出“ECONNREFUSED”错误。

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

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' }

只是想知道这可能是什么原因,我可以使用网络浏览器很好地连接到互联网。

谢谢

4

2 回答 2

0

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

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);
于 2016-12-28T21:45:20.850 回答
0

这可能是因为由于防火墙或代理,该进程无法访问 Internet。

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

于 2016-01-15T06:04:37.640 回答