【发布时间】:2011-06-20 14:08:33
【问题描述】:
我已经按照文章https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29安装了node
现在我在 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)
【问题讨论】:
-
您是否使用 cygwin 控制台中的
node /home/adminstrator/test.js命令启动了 node.js? -
@actual 如果我执行 node test.js 它会显示 sys.puts("Output") 函数的输出。但是如何在浏览器 respose.write 中运行呢?