2012-02-15 101 views
1

我是Node.js的新手。我跟着教程,并键入以下Node.js浏览器错误

var sys = require("util"), 
http = require("http"); 

http.createServer(function(request, response) { 
response.sendHeader(200, {"Content-Type": "text/html"}); 
response.write("Hello World!"); 
response.close(); 
}).listen(8080); 

sys.puts("Server running at http://localhost:1331/"); 

但是当我去我的浏览器和IE http://localhost:1331 它失败浏览的网址时,日erequested URL

越来越打开CMD下输入网址

TypeError: Object #<ServerResponse> has no method 'sendHeader' 
at Server.<anonymous> (D:\node_js\hello.js:11:14) 
at Server.emit (events.js:70:17) 
at HTTPParser.onIncoming (http.js:1511:12) 
at HTTPParser.onHeadersComplete (http.js:102:31) 
at Socket.ondata (http.js:1407:22) 
at TCP.onread (net.js:354:27) 
+0

我需要设置任何网络服务器像apache这个或nodejs设置它? – baig772 2012-02-15 11:38:00

+0

您的node.js应用程序是一个Web服务器。所以不,你不需要另一个网络服务器。看起来你遵循一个(非常)老的教程,请参阅下面的答案。 – 2012-02-15 13:04:01

回答

8

它看起来像你跟着一个过时的教程。 Node API已经改变。试试这个例子:

var http = require('http'); 

http.createServer(function (request, response) { 
    response.writeHead(200, {'Content-Type': 'text/html'}); 
    response.end('Hello World\n'); 
}).listen(1331); 

console.log('Server running at http://127.0.0.1:1331/'); 
0

它看起来像你正在监听端口8080.要么改变你的URL或你传递到listen()端口号。

+0

在listen()中更改了端口但仍然存在:( – baig772 2012-02-15 11:44:13

+0

hmm也许使用netstat或其他东西来确保它在正确的端口上运行,并且没有其他进程正在使用该端口号 – 2012-02-15 11:53:15

+0

我已更改了很多端口号,它应该在其中一个工作 – baig772 2012-02-15 11:59:50