我是Node.js的新手。我按照教程的要求输入了以下内容
var sys = require("util"),
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:1331/");但是,当我转到我的浏览器并输入url,即http://localhost:1331,它无法打开请求的URL
浏览URL时在cmd中获取以下内容
TypeError: Object #<ServerResponse> has no method 'sendHeader'
at Server.<anonymous> (D:\node_js\hello.js:11:14)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1511:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1407:22)
at TCP.onread (net.js:354:27)发布于 2012-02-15 21:02:55
看起来你遵循了一个过时的教程。自那以后,Node API发生了变化。试一下这个例子:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Hello World\n');
}).listen(1331);
console.log('Server running at http://127.0.0.1:1331/');发布于 2012-02-15 19:39:42
看起来您正在监听端口8080。更改您的URL或要传递给listen()的端口号。
https://stackoverflow.com/questions/9292598
复制相似问题