Подтвердить что ты не робот

Node.js ReferenceError

Это мой первый выход в 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
[email protected]:~$

Может ли кто-нибудь указать мне в правильном направлении?

4b9b3361

Ответ 1

require() не работает как #include или import на других языках.

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) {
});

Ответ 2

Как ясно указывает ошибка, нет функции re.

Вы имеете в виду require?