var http = require('http');
var handlerRequest = function(req,res){
res.end('hello');
};
var webServer = http.createServer(handlerRequest);
webServer.listen(9090);
console.log('webServer running on 9090..');
以上代碼在windows上運行沒有錯誤,然后我把代碼上傳到centos虛擬機里,運行后報錯
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1043:14)
at listen (net.js:1065:10)
at Server.listen (net.js:1139:5)
at Object.<anonymous> (/home/test/port8/web.js:8:11)
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)
意思似乎是我沒有對錯誤進行處理,下面說是地址被占用
然后在linux上執行如下語句
[root@nodejs port8]# netstat -lntp | grep 9090
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 24909/node
發現這個9090端口其實已經被占用了。
所以:如果你隨意寫http.listen()的端口,一定要先判斷一下端口是否已經被占用。