2011-12-29 25 views
5

enter image description herenode.js中的process.nextTick錯誤?

我得到node.js這個非常基本的例子process.nextTick錯誤。

有人可以搞清楚嗎? 節點無法啓動端口8000監聽?

# cat nodejs.js 
net = require("net"); 
s = net.createServer(); 

net.on('connection', function (c) { 
c.end('hello'); 
}); 

s.listen(8000); 

# node nodejs.js 

node.js:201 
    throw e; // process.nextTick error, or 'error' event on first tick 
     ^
TypeError: Object #<Object> has no method 'on' 
at Object.<anonymous> (/home/ec2-user/praveen/nodejs.js:4:5) 
at Module._compile (module.js:432:26) 
at Object..js (module.js:450:10) 
at Module.load (module.js:351:31) 
at Function._load (module.js:310:12) 
at Array.0 (module.js:470:10) 
at EventEmitter._tickCallback (node.js:192:40) 

回答

4

看來你想捕捉庫(net)的事件,但你應該看the connectionListener argument to createServer。試試這個:

var net = require("net"); 

var server = net.createServer(function (c) { 
    c.end('Hello!'); // Implicitly fired on 'connection' 
}); 

server.listen(8000); 
+0

是嗎?我已經從瑞安的演講中複製了完全相同的命令.. – 2011-12-29 21:51:00

+0

Node.js變化的速度非常快,以至於不能依賴超過幾個月的材料。不知道該演示文稿是從何時發佈的,但這是您在Node 0.6中的做法。 – sczizzo 2011-12-29 21:53:14

+0

非常感謝..它的工作原理。我想演示文稿很老了。 – 2011-12-29 23:32:22

10

這是Ryan滑動錯字! :-0

s/net.on/s.on/ 
+0

+1是的,這也適用。 – sczizzo 2011-12-30 05:07:15

1

對於其他人,當他們嘗試發出brunch watch --server,檢查並確保你沒有使用相同的端口上運行任何其他服務器誰可能絆倒在這裏尋找原因節點pukes這個錯誤(即在另一個殼中)。