【问题标题】:making server in node.js simple hello world在 node.js 中制作服务器简单 hello world
【发布时间】: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 中运行呢?

标签: cygwin node.js


【解决方案1】:

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

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

【讨论】:

  • @Box9 但是如何在浏览器中打开?
  • @Rahul,抱歉我错过了那部分。在 cygwin 中输入:node /home/administrator/test.js,然后将浏览器定向到localhost:8080。
  • @Rahul,不,apache 完全不同。您说您在 Cygwin 中看到消息“服务器正在运行...”?你还看到什么吗? (在浏览器中输入“localhost:8080”之前和之后。
  • @Rahul,那是因为 sendHeader 也不存在。我已经用正确的方法更新了我的答案,但请检查我链接到的文档!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多