Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top