Javascript nodejs请求-始终返回
我已经使用cheerio、request和longjohn模块编写了一些代码,这些模块昨天工作过,但今天它总是抛出“ECONNREFUSED”错误 我尝试使用使用请求的简单示例代码:Javascript nodejs请求-始终返回,javascript,node.js,request,econnrefused,Javascript,Node.js,Request,Econnrefused,我已经使用cheerio、request和longjohn模块编写了一些代码,这些模块昨天工作过,但今天它总是抛出“ECONNREFUSED”错误 我尝试使用使用请求的简单示例代码: var request = require('request'); request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) {
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' }
只是想知道这可能是什么原因,我可以很好地连接到互联网使用网络浏览器
谢谢这可能是因为进程由于防火墙或代理而无法访问internet
检查防火墙和代理设置,确保进程可以访问internet。这可能是因为进程由于防火墙或代理而无法访问internet
检查防火墙和代理设置,确保进程可以访问internet。我的问题是启动服务器进程后立即执行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);
我的问题是在启动服务器进程后立即执行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);
可能是防火墙设置。谢谢AMIR,我忘记了上次运行此代码时我有一个代理(由于使用公司互联网对抗开发人员)可能是防火墙设置。谢谢AMIR,我忘记了上次运行此代码时我有一个代理(由于使用公司互联网对抗开发人员)