Node.js Error: connect ECONNREFUSED

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

I am new to node and running into this error on a simple tutorial.

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_modules folder.

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:

var makeRequest = require('./make_request');  makeRequest("Here's looking at you, kid"); makeRequest("Hello, this is dog"); 

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; 

回答1:

Chances are you are struggling with the node.js dying whenever the server you are calling refuses to connect. Try this:

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.



回答2:

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.



回答3:

I was having the same issue with ghost and heroku.

heroku config:set NODE_ENV=production  

solved it!

Check your config and env that the server is running on.



回答4:

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.

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...'); 


回答5:

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

mongoose.connect("mongodb://localhost:27017/demoDb");

after i started the mongodb server with the command mongod this error is gone



回答6:

People run into this error when the Node.js process is still running and they are attempting to start the server again. Try this:

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.



回答7:

Check with starting mysql in terminal. Use below command

mysql-ctl start

In my case its worked



回答8:

If you are on MEAN (Mongo-Express-AngularJS-Node) stack, run mongod first, and this error message will go away.



回答9:

The Unhandled 'error' event is referring not providing a function to the request to pass errors. Without this event the node process ends with the error instead of failing gracefully and providing actual feedback. You can set the event just before the request.write line to catch any issues:

request.on('error', function(err) {     console.log(err); }); 

More examples below:

https://nodejs.org/api/http.html#http_http_request_options_callback



回答10:

In my case the error was because i was using a different version of Node.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!