gpt4 book ai didi

Node.js 浏览器错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:55 25 4
gpt4 key购买 nike

我是 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无法打开请求的 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)

最佳答案

看来您遵循的是过时的教程。 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/');

关于Node.js 浏览器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9292598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com