Javascript Node.js 错误:连接 ECONNREFUSED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14168433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js Error: connect ECONNREFUSED
提问by ian
I am new to node and running into this error on a simple tutorial.
我是 node 的新手,在一个简单的教程中遇到了这个错误。
I am on the OS X 10.8.2 trying this from CodeRunner and the Terminal.
I have also tried putting my module in the node_modulesfolder.
我在 OS X 10.8.2 上从 CodeRunner 和终端尝试这个。我也试过把我的模块放在node_modules文件夹中。
I can tell this is some kind of connection problem but I have no idea why?
我可以说这是某种连接问题,但我不知道为什么?
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:770:11)
at Object.afterConnect [as oncomplete] (net.js:761:19)
app.js:
应用程序.js:
var makeRequest = require('./make_request');
makeRequest("Here's looking at you, kid");
makeRequest("Hello, this is dog");
make_request.js:
make_request.js:
var http = require('http');
var makeRequest = function(message) {
//var message = "Here's looking at you, kid.";
var options = {
host: 'localhost', port: 8080, path:'/', method: 'POST'
}
var request = http.request(options, function(response) {
response.on('data', function(data) {
console.log(data);
});
});
request.write(message);
request.end();
};
module.exports = makeRequest;
回答by user2957009
Chances are you are struggling with the node.js dying whenever the server you are calling refuses to connect. Try this:
每当您调用的服务器拒绝连接时,您就有可能正在为 node.js 死掉而苦苦挣扎。尝试这个:
process.on('uncaughtException', function (err) {
console.log(err);
});
This keeps your server running and also give you a place to attach the debugger and look for a deeper problem.
这使您的服务器保持运行,并且还为您提供了一个附加调试器并查找更深层次问题的地方。
回答by Siddhartha Mukherjee
I was having the same issue with ghost and heroku.
我在使用 ghost 和 heroku 时遇到了同样的问题。
heroku config:set NODE_ENV=production
solved it!
解决了!
Check your config and env that the server is running on.
检查服务器正在运行的配置和环境。
回答by JakubKnejzlik
You're trying to connect to localhost:8080 ... is any service running on your localhost and on this port? If not, the connection is refused which cause this error. I would suggest to check if there is anything running on localhost:8080 first.
您正在尝试连接到 localhost:8080 ... 是否有任何服务在您的 localhost 和此端口上运行?如果不是,则连接被拒绝,从而导致此错误。我建议先检查是否有任何东西在 localhost:8080 上运行。
回答by Andrew M.
You need to have a server running on port 8080 when you run the code above that simply returns the request back through the response. Copy the code below to a separate file (say 'server.js') and start this server using the node command (node server.js). You can then separately run your code above (node app.js) from a separate command line.
当您运行上面的代码时,您需要在端口 8080 上运行服务器,该代码仅通过响应返回请求。将下面的代码复制到一个单独的文件(比如“server.js”)并使用 node 命令(node server.js)启动这个服务器。然后,您可以从单独的命令行单独运行上面的代码 (node app.js)。
var http = require('http');
http.createServer(function(request, response){
//The following code will print out the incoming request text
request.pipe(response);
}).listen(8080, '127.0.0.1');
console.log('Listening on port 8080...');
回答by pashaplus
Sometimes it may occur, if there is any database connection in your code but you did not start the database server yet.
有时可能会发生,如果您的代码中有任何数据库连接但您尚未启动数据库服务器。
Im my case i have some piece of code to connect with mongodb
我的情况我有一些代码可以与 mongodb 连接
mongoose.connect("mongodb://localhost:27017/demoDb");
mongoose.connect("mongodb://localhost:27017/demoDb");
after i started the mongodb server with the command mongodthis error is gone
在我使用命令mongod启动 mongodb 服务器后,此错误消失了
回答by Bob Vargas
If you are on MEAN (Mongo-Express-AngularJS-Node) stack, run mongod first, and this error message will go away.
如果您在 MEAN (Mongo-Express-AngularJS-Node) 堆栈上,请先运行 mongod,此错误消息将消失。
回答by mujaffars
Check with starting mysql in terminal. Use below command
检查在终端中启动 mysql。使用下面的命令
mysql-ctl start
mysql-ctl start
In my case its worked
在我的情况下它的工作
回答by rahulmehta95
People run into this error when the Node.js process is still running and they are attempting to start the server again. Try this:
当 Node.js 进程仍在运行并且他们试图再次启动服务器时,人们会遇到此错误。尝试这个:
ps aux | grep node
ps aux | grep node
This will print something along the lines of:
这将打印以下内容:
user 7668 4.3 1.0 42060 10708 pts/1 Sl+ 20:36 0:00 node server
user 7749 0.0 0.0 4384 832 pts/8 S+ 20:37 0:00 grep --color=auto node
In this case, the process will be the one with the pid 7668. To kill it and restart the server, run kill -9 7668.
在这种情况下,进程将是 pid 为 7668 的进程。要杀死它并重新启动服务器,请运行kill -9 7668。
回答by Jai
Same error occurs in localhost, i'm just changing the mysql port (8080 into localhost mysql port 5506). it works for me.
同样的错误发生在 localhost 中,我只是将 mysql 端口(8080 更改为 localhost mysql 端口 5506)。这个对我有用。
回答by shamila
Run server.js from a different command line and client.js from a different command line
从不同的命令行运行 server.js 并从不同的命令行运行 client.js

