2016-07-08 42 views
0

我想這是我的第一個代碼,我得到錯誤開始學習節點羣集收到錯誤

const cluster = require('cluster'); 
    const http = require('http'); 
    const numCPUs = 4; 

    if (cluster.isMaster) { 
     // Fork workers. 
     for (var i = 0; i < numCPUs; i++) { 
     cluster.fork(); 
     } 
     cluster.on('online', function(worker) { 
      console.log('Worker ' + worker.process.pid + ' is online'); 
     }); 

     cluster.on('exit', (worker, code, signal) => { 
     console.log(`worker ${worker.process.pid} died`); 
     }); 

    } else { 
     // Workers can share any TCP connection 
     // In this case it is an HTTP server 
     http.createServer((req, res) => { 
     res.writeHead(200); 
     res.end('hello world\n'); 
     }).listen(8000); 
    } 

這是我收到的錯誤: -

工人11056在線 工人11057 is online Worker 11058 is online Worker 11059 is online events.js:141 throw er; //未處理 '錯誤' 事件 ^

錯誤:綁定EADDRINUSE空:8000 在Object.exports._errnoException(util.js中:870:11) 在exports._exceptionWithHostPort(util.js中:893:20) at工作人員處的rr(cluster.js:594:14) 處的cb(net.js:1302:16) 。 (cluster.js:564:9) 正在處理。 (cluster.js:714:8) at processingTem(events.js:92:20) at process.emit(events.js:172:7) at handleMessage(internal/child_process.js:689:10) 在Pipe.channel.onread(internal/child_process.js:440:11) events.js:141 throw er; //未處理 '錯誤' 事件 ^

錯誤:綁定EADDRINUSE空:8000 在Object.exports._errnoException(util.js中:870:11) 在exports._exceptionWithHostPort(util.js中:893:20) at工作人員處的rr(cluster.js:594:14) 處的cb(net.js:1302:16) 。 (cluster.js:564:9) 正在處理。 (cluster.js:714:8) at processingTem(events.js:92:20) at process.emit(events.js:172:7) at handleMessage(internal/child_process.js:689:10) 在Pipe.channel.onread(internal/child_process.js:440:11) events.js:141 throw er; //未處理 '錯誤' 事件 ^

錯誤:綁定EADDRINUSE空:8000 在Object.exports._errnoException(util.js中:870:11) 在exports._exceptionWithHostPort(util.js中:893:20) at工作人員處的rr(cluster.js:594:14) 處的cb(net.js:1302:16) 。 (cluster.js:564:9) 正在處理。 (cluster.js:714:8) at processingTem(events.js:92:20) at process.emit(events.js:172:7) at handleMessage(internal/child_process.js:689:10) 在Pipe.channel.onread(internal/child_process.js:440:11) 工人11056死亡 工人11057死亡 工人11058死亡 events.js:141 throw er; //未處理 '錯誤' 事件 ^

錯誤:綁定EADDRINUSE空:8000 在Object.exports._errnoException(util.js中:870:11) 在exports._exceptionWithHostPort(util.js中:893:20) at工作人員處的rr(cluster.js:594:14) 處的cb(net.js:1302:16) 。 (cluster.js:564:9) 正在處理。 (cluster.js:714:8) at processingTem(events.js:92:20) at process.emit(events.js:172:7) at handleMessage(internal/child_process.js:689:10) 在Pipe.channel。onread(internal/child_process.js:440:11) worker 11059 died

回答

0

EADDRINUSE意味着別的東西已經綁定到您嘗試使用的端口/ IP地址組合。

檢查事項:

  • 你已經有一些其他進程綁定到端口8000?如果是這樣,也許在這裏更改代碼中的端口號。

  • 您是否有另一個此代碼的實例運行? (在類UNIX操作系統上,可能使用ps以及適當的選項來查找它。)如果是,則終止其他實例。 (在UNIX類操作系統,您可以使用killpkill用適當的選項。)

+0

可以請你建議我怎麼???一些鏈接或例子? –

+0

我已經用建議更新了我的答案。 (我的第一個回答基本上是錯誤的,對此抱歉,我已經刪除了不正確的信息,代碼中沒有錯誤,我的錯誤) – Trott

+0

沒有其他進程使用此端口8000.我試過更改港口,現在它正在工作 –