【发布时间】:2018-08-29 12:19:28
【问题描述】:
我正在尝试运行一个非常简单的节点文件,它曾经可以工作,但现在我收到了这个错误。
events.js:160
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:3002
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)
问题是,如果我在这个端口上运行另一个应用程序,就没有问题。即使我更改了文件中的端口,错误仍然存在。
文件代码:
var http = require('http');
var opcoes = {
hostname: 'localhost',
port: 3002,
path: '/',
method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
//Content-type
var html = 'nome=José'; //x-www-form-urlencoded
var json = { nome: 'José' };
var string_json = JSON.stringify(json);
var buffer_corpo_response = [];
var req = http.request(opcoes, function(res) {
res.on('data', function(pedaco) {
buffer_corpo_response.push(pedaco);
});
res.on('end', function() {
var corpo_responde = Buffer.concat(buffer_corpo_response).toString();
console.log(corpo_responde);
});
});
req.write(string_json);
req.end();
【问题讨论】:
-
您的本地机器是否出现此错误?
-
是的,它正在我的本地机器上运行
-
您正在查询的应用程序是否正在运行并在此端口上侦听?或者 localhost:3002 是否需要身份验证?
-
它只是运行和聆听。它不需要任何身份验证
标签: node.js