Hello World in NodeJS

Muskan Bararia
Sep 6, 2018 · 2 min read

NodeJS is one of the many must have skills for a programmer. As a beginner, I always found NodeJS confusing and was always struck at one point or the other. So I decided to compose a tutorial for NodeJS for newbies, which would familiarise them with this run time environment without hurting their comfort zone.

In this series of tutorials, I will try to relate with C as much as I can, because that’s the mother tongue of almost every programmer. However, all these relationships are for purpose of general understanding and henceforth, are not technically accurate. So let’s begin with our first program in NodeJS (or C ?). Let’s print Hello World! . But wait, where should we print? How about on your browser?

Importing http module

In C, we need #include<stdio.h> to begin our program. stdio is a library.

In NodeJS, we need require(http) to initialize our server. http is a module. We will learn more about modules in future exercises.

Include this code in your first node program to get you started.

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

First, We are importing http module to initialize the server. Afterwards, we are defining the host and the port where we want to see the output.

Creating server object

In C, we print Hello World inside int main() function.

In NodeJS, we will print Hello World inside const server using createServer method.

Include the following code in your program to achieve this.

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

Here, We are creating a server object to store createServer method. res.statusCode=200 => We will begin with setting the response code as 200, which indicates that everything is okay. res.setHeader Afterwards, we are setting the header of response as text, since we are printing text type content. res.end('Hello World!\n') Here comes our Hello World.

Initializing server

We will initialize our server with the host and port we provided earlier. We can print some message on console to ensure the proper working of our program.

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

Executing and checking the output

In C, we compile and execute the code to see the output. In NodeJS, we will initiate the file and browse to port to see the output.

To initiate the file, navigate to hello-node:

cd hello-node/

Afterwards, type the following command:

node hello-node.js

Voila! You must see an output like this:

Server running at http://127.0.0.1:3000/

It means everything is working fine. You can open this link in your browser to see the output.

To close the server, simply press Ctrl+C.

Congratulations, you have successfully printed Hello World using NodeJS.

You can also visit my repository for further insight and new tutorials.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade