我是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/");
但是当我访问我的浏览器并输入网址时,http://localhost:1331 它未能打开该网址
在浏览网址时在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)
答案 0 :(得分:8)
您好像遵循了过时的教程。 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/');
答案 1 :(得分:0)
看起来你正在侦听端口8080.要么改变你传入listen()的URL或端口号。