尝试 HTTP 请求时连接 ECONNREFUSED 127.0.0.1:80

新手上路,请多包涵

我正在尝试使用本机 node.js http 模块向 news.google.com 发出 http 请求。我在尝试以下操作时收到 connect ECONNREFUSED 127.0.0.1:80 错误

var http = require('http');

var payload = JSON.stringify({
    name: 'John Smith',
    email: 'john.smith@smith.com',
    resume: 'https://drive.google.com/open?id=asgsaegsehsehseh'
});

var options = {
    hostname: 'https://news.google.com',
    path: '/',
    method: 'GET'
};

var httpRequest = http.request(options, function(request, response) {
    console.log('STATUS', response.statusCode);
    response.setEncoding('utf8');

    response.on('data', function(chunk) {
        console.log('BODY:', chunk);
    });

    response.on('end', function() {
        console.log('No more data in response');
    });
});

httpRequest.on('error', function(e) {
    console.log('Error with the request:', e.message);
});

httpRequest.write(payload);
httpRequest.end();

为什么我会收到此错误?

我尝试使用 request npm 模块。它奏效了!

原文由 ng-hacker-319 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
1 个回答

我对 jestsupertest 有同样的问题。刚刚将 api/blogs 更改为 /api/blogs 并且有效!

原文由 Abdelhk 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏