1

I am trying to take the first command line argument as the port to run node on. The following code is not working in node.js:

try {

    if(process.argv[2] == undefined) {
        throw new Error("no port specified");
    }
    var port = process.argv[0];
    console.log(port);
}
catch (err) {
    console.log("Error give port number as the argument");
    return;
}
require('http').createServer(function handleRequest(req, res) {
    res.writeHead(200, {'content-type' : 'text/plain'});
    res.end('Hello World!');
}).listen(port);

It gives the following error:

$ node server.js 8080
node

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:884:11)
    at Server._listen2 (net.js:1003:19)
    at listen (net.js:1044:10)
    at Server.listen (net.js:1104:5)
    at Object.<anonymous> (/home/studiet/Documents/Aptana Studio 3 Workspace/nodef2b/server.js:19:4)
    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)

I know that node.js is asyncronous, but in this case I thought nothing was waiting for anything. What is wrong? How can I make the system wait for whatever is taking too long here, and what is taking too long? Or, what else is wrong?

EDIT: line 19 in the code is where I .listen(port);

1
  • By the error Error: listen EADDRINUSE, I'm lead to believe there's already an application listening on that port. Commented May 8, 2013 at 22:02

1 Answer 1

5

EADDRINUSE stands for Error Address In Use.

Some other process is using port 80.

You need to find that process and terminate it with extreme prejudice.

EDIT: In this case, it means an invalid port number.
As you can see from your first line of output, port is "node", not 8080.
You need to set it to argv[2].

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried 8080, 8081, 8082 and 80123. All give EADDRINUSE. What to do? Ubuntu. And never 80 I know that is not allowed
Thanks! I see that I check for argv[2] but use argv[0].

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.