Report this

What is the reason for this report?

I can't make it to work with the Hello.js

Posted on August 8, 2016

following this video: https://www.youtube.com/watch?v=Jsmeh7q9Qv4

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://10.130.16.227:8080/');

//error after issuing a command "node hello.js"
TypeError: http.createServer is not a function
    at Object.<anonymous> (/root/hello.js:3:6)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

Any help please!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Your code works fine for me. Two things to check

  1. is your node up to date? ie, what does ‘node -v’ give ?
  2. could node be confused what ‘http’ is referring to? what if you place your script in ‘/tmp/’ and run it from there?

Hello.js is now working by changing the script to this script below.

‘use strict’; const http = require(‘http’); var server = http.createServer((req, res) => { res.writeHead(200, {‘Content-type’:‘text/html’}); res.end(‘<h1>Hello NodeJS</h1>’); }).listen(8080, ‘10.133.16.227’);

server.listen(8080,() => console.log(‘Server running on port 10.133.16.227:8080’));

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.