Hello World in Nodejs

Question:

How do i create Hello World script in Nodejs?

Applicable to:

  • VPS Linux
  • Dedicated Server Linux
  • EVC Linux

Answer:

Before start, please make sure you already install nodejs and npm. Let's go to /root/bin for example. Name a file server.js

In this section, nodejs will deploy custom port 3000 and listening all network interfaces.

const http = require('http');

const hostname = '0.0.0.0'; // Listen on all network interfaces
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World from Exabytes!\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

And then run it.

# node server.js
Server running at http://0.0.0.0:3000/

Check at your browser and open IP server with port 3000

You can change to random ports as your project needs.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.