使node.js中的服务器简单的hello world
我已经通过以下文章安装了节点https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29
现在我已经在c:/cygwin/home/adminstrator/test.js中创build了一个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/");
但在浏览器中如果运行本地主机:8080或8.8.8.8:8080内容不显示(“你好世界”)
请build议我该怎么办?
运行命令时编辑错误
$ 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)
方法.sendHeader()
和.close()
不存在。 你.writeHead()
和.end()
来代替。
参考: http : //nodejs.org/docs/v0.3.7/api/http.html#http.ServerResponse