1
響應我有這個小程序的一個問題:node.js錯誤:連接ECONNREFUSED;從服務器
var http = require("http");
var request = http.request({
hostname: "localhost",
port: 8000,
path: "/",
method: "GET"
}, function(response) {
var statusCode = response.statusCode;
var headers = response.headers;
var statusLine = "HTTP/" + response.httpVersion + " " +statusCode + " " + http.STATUS_CODES[statusCode];
console.log(statusLine);
for (header in headers) {
console.log(header + ": " + headers[header]);
}
console.log();
response.setEncoding("utf8");
response.on("data", function(data) {
process.stdout.write(data);
});
response.on("end", function() {
console.log();
});
});
結果在控制檯是這樣的:
events.js:141 throw er; // Unhandled 'error' event ^ Error: connect ECONNREFUSED 127.0.0.1:8000 at Object.exports._errnoException (util.js:870:11) at exports._exceptionWithHostPort (util.js:893:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
我不明白爲什麼會這樣。
你想用代碼實現什麼? – gnerkus
發生這種情況的原因是它無法連接到端點 - 在這種情況下,無法訪問到http:// localhost:8000' – charliebrownie
你應該在localhost:8000上運行一些東西嗎?因爲它看起來不像你現在做的。 – philnash