node.js Error: connect ECONNREFUSED; response from server(node.js 错误:连接 ECONNREFUSED;来自服务器的响应)
问题描述
I have a problem with this little program:
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();
});
});
The result in console is this:
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)
I do not understand why this happens.
From your code, It looks like your file contains code that makes get request to localhost (127.0.0.1:8000).
The problem might be you have not created server on your local machine which listens to port 8000.
For that you have to set up server on localhost which can serve your request.
Create server.js
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); // This will serve your request to '/'. }); app.listen(8000, function () { console.log('Example app listening on port 8000!'); });
Run server.js : node server.js
Run file that contains code to make request.
这篇关于node.js 错误:连接 ECONNREFUSED;来自服务器的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:node.js 错误:连接 ECONNREFUSED;来自服务器的响应
基础教程推荐
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01