在node.js中制作服务器简单的hello world

时间:2011-02-03 10:47:45

标签: cygwin node.js

我按照文章https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29

安装了节点

现在我在c:/cygwin/home/adminstrator/test.js中创建了一个test.js文件,这里有我的节点文件夹。

我的test.js文件是

var sys = require("sys"),  
    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:8080/"); 

但是在浏览器中如果运行localhost:8080或8.8.8.8:8080则内容未显示(“hello world”)

请建议我该怎么办?

运行命令

时出现

编辑错误

$ node /home/Administrator/test.js
Server running at http://localhost:8080/

/home/Administrator/test.js:5
     response.sendHeader(200, {"Content-Type": "text/html"});
              ^
TypeError: Object #<a ServerResponse> has no method 'sendHeader'
    at Server.<anonymous> (/home/Administrator/test.js:5:15)
    at Server.emit (events.js:27:15)
    at HTTPParser.onIncoming (http.js:871:14)
    at HTTPParser.onHeadersComplete (http.js:88:31)
    at Stream.ondata (http.js:792:22)
    at Stream._onReadable (net.js:762:27)
    at IOWatcher.onReadable [as callback] (net.js:276:10)

1 个答案:

答案 0 :(得分:3)

方法.sendHeader().close()不存在。你不想.writeHead().end()

参考:http://nodejs.org/docs/v0.3.7/api/http.html#http.ServerResponse