我想把第一個命令行參數作爲運行節點的端口。 下面的代碼是不是在Node.js的工作:什麼是阻止我的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);
它提供了以下錯誤:
$ 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)
我知道的node.js是asyncronous,但在這種情況下,我認爲沒有什麼在等待爲了任何東西。哪裏不對?我該如何讓這個系統等待在這裏花費太長時間的東西,什麼花費太長時間?或者,還有什麼錯誤?
編輯:代碼中的第19行是我.listen(port);
由錯誤'錯誤:聽EADDRINUSE',我導致相信已經有一個應用程序偵聽該端口。 – 2013-05-08 22:02:09