‘Hello World’ in Node.js

Hello world webpage just in Node.js.

Mohammed Ijas
4 min readJun 20, 2020
Cool Node.js wallpaper made by me[link]

Before diving in, you should know the basic concepts of JavaScript.

What is Node.js ? 🤔

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser.

Node is an asynchronous event driven JavaScript runtime built upon Chrome’s V8 JavaScript engine. It’s designed to build scalable network applications.

Actually, JavaScript was made for just the front-end but now it can be used in server side with the help of Node.js.

Asynchronous means, handling events without interrupting the main thread because JavaScript is single threaded, meaning there is only one thread of execution.

Let’s get going. 🚶

Step 1:

You should have Node.js installed in your PC to use it(Quite obvious).

Goto : https://nodejs.org/en/
Download and install Node.js according to your OS. It’s super easy to install.

Node.js official website

Step 2:

Create a folder somewhere on your PC and open terminal or cmd or whatever and set the directory to that folder.(I am using cmd in Windows10)

C:\Users\Ijas\Desktop> mkdir mySite
C:\Users\Ijas\Desktop>cd mySite
C:\Users\Ijas\Desktop\mySite>

cd => change directory.
mkdir => make directory

Step 3:

Now we need to initialize our project. Run the following to do that:

> npm init

npm => Node Package Manager

This is what you might see next:

C:\Users\Ijas\Desktop\mySite>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (mysite)

Fill it accordingly or just go with default.

This is mine: 👐

{
“name”: “hello_world”,
“version”: “1.0.0”,
“description”: “Hello world in Node.js”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“author”: “ijas”,
“license”: “ISC”
}

Now if you check your current directory, you can see a package.json file with all the above details.(you can change the values in it).

Step 4:

Create a file named index.js and open it and fill it like this 👇.

const http = require(‘http’);const hostname = ‘localhost’;
const port = 3000;
const server = http.createServer((req, res) => {
console.log(req.headers);
res.statusCode = 200;
res.end(‘<html><body><h1>Hello, World!</h1></body></html>’);
})
server.listen(port, hostname);

Now in cmd run :

> node index.js

This will start a test server at localhost , http://localhost:3000/, goto this url to view the page.

I will now explain what’s in the code: 👇

const http = require(‘http’); => this will import http module in Node.

const hostname = ‘localhost’;
const port = 3000;
This above is used to initialize hostname to localhost and port to 3000.

console.log(req.headers); => this is used to show the geeky details on your terminal when the server starts.

http.createServer => this turns your computer into an HTTP server.
We pass a function as argument in it with arguments res and req.
In the code you see a JavaScript arrow function.

res.end() => will end the response process. This method actually comes from Node core, specifically the response.end() method of http.ServerResponse. It is used to quickly end the response without any data.
You can pass a string argument to add as the response body as we have did that here.

res.statusCode = 200 => This is used to sent back HTTP status code. 200 is code for OK.

server.listen(port, hostname); => Create a server that listens on port 3000 of your computer.

That’s it.

Additional tips:

✔️ You can also use npm start to start the server.
For that you need to add an additional line at script in package.json

“scripts”: {“test”: “echo \”Error: no test specified\” && exit 1",“start”:”node index”},
Example package.json file

Now you can start server using npm start

✔️ Hot Reload.
You need to restart the server every time when you change anything in the code and it’s quite annoying. For that we use hot reload, this will automatically reset the server when you make changes to the code.

For this we need to install a package called “nodemon”.Just execute the following to use nodemon:

npm install -g nodemon

Now just execute nodemon startto start the server. You need to add start in package.json like in the above tip to make this work.

That’s it for now. Feel free to share and clap. 💛

--

--

Mohammed Ijas

Am I a magician? No, I am a guy who know many things, mostly related to computers and programming.