105

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;
ian
  • 10,995
  • 25
  • 67
  • 96
  • I also put this code up on `https://c9.io/` and get the same error. – ian Jan 05 '13 at 04:59
  • Open for incoming our out coming or what? Same issue on a node server using the service defined ports and host. If you actually know what is going on here post a real solution not vague comments as stated in the question I am new to node. – ian Jan 05 '13 at 06:43
  • 2
    @ian, Nowhere in your cod are you creating a server. So, what do you have running on port 8080 that you are trying to connect to? – Brad Jan 05 '13 at 15:49
  • 2
    For any future readers I was trying to make an API request to a PHP server and my request port was not set to 80. Once changed my http requests worked fine. – Michael J. Calkins Nov 11 '13 at 18:24
  • I was running node 17 (on a M1 Mac) and I removed it via `brew uninstall node` and then install node 16 with `brew install node@16`. Worked a treat. I _may_ have had to `brew link --force node@16` but I can't remember right now. – Joshua Pinter Jan 22 '22 at 02:21

14 Answers14

58

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.

user2957009
  • 772
  • 5
  • 10
  • 28
    Keeping your server running after an uncaught exception is a dangerous thing to do. You do not know if the exception left your server in an expected state. It's much safer to kill the server and have another process restart it (and log the exception, obviously). – Martin Epsz Oct 08 '14 at 15:24
  • 3
    In general its not a good idea to catch all exceptions in the dark. Take a look at [this answer](http://stackoverflow.com/a/7590530/2295964). – Yan Foto Sep 04 '15 at 14:12
  • 7
    It depends on what you are trying to accomplish. I was writing a monitoring app where it is critical to keep up and keep moving. It seems the OP was struggling with just finding out what the issue was. All the other answers are about specific issues that could be the cause of this error. As a debugging step the above is almost always necessary before you can drill down to the real issue. It is also weird to me that you are OK with your server dying because of one failure, causing all other client calls to start failing? – user2957009 Sep 29 '15 at 14:16
11

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.

JakubKnejzlik
  • 6,043
  • 3
  • 36
  • 40
11

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.

Dan
  • 5,015
  • 4
  • 30
  • 41
Siddhartha Mukherjee
  • 6,010
  • 3
  • 21
  • 11
6

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...');
Andrew M.
  • 81
  • 1
  • 7
6

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

pashaplus
  • 3,150
  • 2
  • 25
  • 24
5

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

Bob Vargas
  • 111
  • 2
  • 6
2

ECONNREFUSED error means that connection could not be made with the target service (in your case localhost:8080). Check your service running on port 8080.

To know more about node.js errors, refer this doc.

1

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.

rahulmehta95
  • 538
  • 4
  • 15
  • 5
    That's not the problem here. He's getting connection refused when trying to make a request. – Brad Jan 05 '13 at 04:41
1

Check with starting mysql in terminal. Use below command

mysql-ctl start

In my case its worked

mujaffars
  • 1,374
  • 1
  • 14
  • 35
1

Run server.js from a different command line and client.js from a different command line

shamila
  • 1,160
  • 6
  • 16
  • 43
1

For me it was a problem in library node-agent-base v4.x which was requested by https-proxy-agent which in it's side was requested by newrelic v5.x library.

The problem was that node-agent-base v4.x for some marvellous idea decided to patch core https.get request (You can see this in the file patch-core.js in the repo). And when I used library which use https.get, it was failed because node-agent-base v4.x have changed function signature, which gave me request to 127.0.0.1 as initial url was lost...

I've fixed this by updating to the next version of newrelic 6.x, where node-agent-base v4.x doesn't has such 'patches'.

This is just crazy...hope my response will save you some time, I've spent several days to debug this.

KorbenDallas
  • 783
  • 7
  • 14
0

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

SmujMaiku
  • 396
  • 3
  • 10
0

Had a similar issue, it turned out the listening port printed was different from what it actually was. Typos in the request string or listening function might make the target server appear to not exist.

-1

I was having the same issue in my dev env with Postman when checking the API. I searched literally all the answers came from google, but no avail.

SOLUTION: I have just moved to "insomnia" (an alternative to Postman) and it works just fine. Hope this workaround may help.