首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js浏览器错误

Node.js浏览器错误
EN

Stack Overflow用户
提问于 2012-02-15 19:35:26
回答 2查看 1.3K关注 0票数 1

我是Node.js的新手。我按照教程的要求输入了以下内容

代码语言:javascript
复制
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中获取以下内容

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-15 21:02:55

看起来你遵循了一个过时的教程。自那以后,Node API发生了变化。试一下这个例子:

代码语言:javascript
复制
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/');
票数 8
EN

Stack Overflow用户

发布于 2012-02-15 19:39:42

看起来您正在监听端口8080。更改您的URL或要传递给listen()的端口号。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9292598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档