首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node.js ReferenceError

Node.js ReferenceError
EN

Stack Overflow用户
提问于 2013-07-07 10:48:47
回答 4查看 45.6K关注 0票数 21

这是我在NodeJS的第一次尝试。我已经在DigitalOcean的一个实例上成功地安装了它。

我有以下helloworld.js

require("http");

http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);console.log('Hello world');

当我通过"node helloworld.js“运行它时,我得到了以下错误:

/home/jason/helloworld.js:4
http.createServer(function(request, response) {
^
ReferenceError: http is not defined
at Object.<anonymous> (/home/jason/helloworld.js:4:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
jason@do:~$

谁能给我指个方向?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-07 13:24:51

require()不像其他语言中的#includeimport那样工作。

require()返回对已解析模块的引用。必须将该引用赋值给变量。

var http = require('http'); //the variable doesn't necessarily have to be named http
http.createServer(function(req, res) {});

require('http').createServer(function(req, res) {
});
票数 51
EN

Stack Overflow用户

发布于 2013-07-07 10:50:43

正如错误清楚地指出的那样,没有re函数。

你是说require吗?

票数 0
EN

Stack Overflow用户

发布于 2018-10-13 20:05:15

我也遇到了同样的问题,我尝试了一下:

var http = require('http');
http.createServer(function (request, response) {

   response.writeHead(200, {'Content-Type': 'text/plain'});

   response.end('Server started\n');
}).listen(8081);

console.log('Server running at http://127.0.0.1:8081/');

也许它会对你有所帮助。

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

https://stackoverflow.com/questions/17508815

复制
相关文章

相似问题

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