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
4

2 Answers 2

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.

2
  • erm. i had another node process running.
    – user883807
    Apr 28, 2014 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. Dec 9, 2014 at 9: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.