Node.js浏览器错误

bai*_*772 1 node.js

我是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/");
Run Code Online (Sandbox Code Playgroud)

但当我进入我的浏览器并输入网址即http:// localhost:1331时 ,无法打开该网址

浏览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)
Run Code Online (Sandbox Code Playgroud)

Lin*_*iel 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/');
Run Code Online (Sandbox Code Playgroud)