сделать сервер в node.js простым привет миром - PullRequest
1 голос
/ 03 февраля 2011

Я установил узел по следующей статье https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29

Теперь я создал файл test.js в каталоге c: /cygwin/home/adminstrator/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 Ответ

3 голосов
/ 03 февраля 2011

Методы .sendHeader() и .close() не существуют. Вы не хотите .writeHead() и .end() вместо.

Ссылка: http://nodejs.org/docs/v0.3.7/api/http.html#http.ServerResponse

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...