Node.js ReferenceError

Jas*_*man 19 node.js

这是我第一次去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');
Run Code Online (Sandbox Code Playgroud)

当我通过"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:~$
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向吗?

c.P*_*.u1 48

require()在其他语言中不起作用#includeimport不起作用.

require()返回对已解析模块的引用.必须将该引用分配给变量.

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

要么

require('http').createServer(function(req, res) {
});
Run Code Online (Sandbox Code Playgroud)