1

I am getting this Error: listen EADDRINUSE when I run the following simpleJSON.js script:

var http = require('http'),
    fs = require('fs');

function handle_incoming_request(req, res) {
    console.log("Incoming request: (" + req.method + ") " + req.url);

    load_album_list(function(err, albums) {
        if (err != null) {
            res.writeHead(503, { "Content-Type" : "application/json" });
            res.end(JSON.stringify({ error: "file_error", message: err.message }) + "\n");
            return;
        }

        res.writeHead(200, { "Content-Type" : "application/json" });
        res.end(JSON.stringify({ error: null, data: { albums: albums }}) + "\n");
    });
}

function load_album_list (callback) {
    fs.readdir('albums/', function (err, file_list) {
        callback(err, file_list);
    });
}

var s = http.createServer(handle_incoming_request);
s.listen(8080);  

Here is the full error I see in my terminal :

$ node simpleJSON.js 

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/max/dev/livelessons/photoApp/simpleJSON.js:26:3)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

And here is my directory structure:

$ tree
.
├── albums
│   ├── australia2012
│   │   ├── picture_01.jpg
│   │   ├── picture_02.jpg
│   │   ├── picture_03.jpg
│   │   ├── picture_04.jpg
│   │   ├── picture_05.jpg
│   │   ├── picture_06.jpg
│   │   └── picture_07.jpg
│   ├── italy2012
│   │   ├── picture_01.jpg
│   │   ├── picture_02.jpg
│   │   ├── picture_03.jpg
│   │   ├── picture_04.jpg
│   │   ├── picture_05.jpg
│   │   ├── picture_06.jpg
│   │   └── picture_07.jpg
│   └── japan2010
│       ├── picture_01.jpg
│       ├── picture_02.jpg
│       ├── picture_03.jpg
│       ├── picture_04.jpg
│       ├── picture_05.jpg
│       ├── picture_06.jpg
│       └── picture_07.jpg
└── simpleJSON.js

4 directories, 22 files
  • 1
    The error means that another process is already listening on port `8080` and Node can't also listen to it. You'll either need to stop the process that's using it, if you can, or choose another port that isn't in use. – Jonathan Lonowski Apr 28 '14 at 23:26
  • possible duplicate of [nodejs Error: listen EADDRINUSE](http://stackoverflow.com/questions/9898372/nodejs-error-listen-eaddrinuse) and [others](http://stackoverflow.com/search?q=Error%3A+listen+EADDRINUSE). – Jonathan Lonowski Apr 28 '14 at 23:28
  • yes you were right. thanks. wish i could close this question. –  Apr 28 '14 at 23:46
  • See a similar issue at http://stackoverflow.com/questions/8553957/how-to-release-localhost-from-error-listen-eaddrinuse/31072560#31072560 – Manohar Reddy Poreddy Jun 26 '15 at 11:59

2 Answers2

4

Has it worked before? It's possible that there is still a leftover process that is connected to the port 8080. Try changing the port number to see if that is the problem.

Joshua Nelson
  • 581
  • 2
  • 10
  • erm. i had another node process running. –  Apr 28 '14 at 23:44
  • so, I rebooted the VM Instance and its working great. I was wondering if there is any way I could stop the connected process. doing a ps wasn't enuf. – Abhishek Deb Dec 09 '14 at 09:22
0

kill the running process of 8080 port and restart the node server. this is because your port 8080 is already busy in other process.

Dheer
  • 773
  • 1
  • 6
  • 16