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);
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].