Getting started with Node.js (Part-2)

Akash Jadhav
3 min readJun 3, 2019

--

Introduction -
Write your first Node.js program to get started with the Node journey!

If you need the list of all node.js tutorials available and their end results follow the link below -
Node.js Tutorials Summary (Part-0)
https://medium.com/@akash_jadhav/node-js-tutorials-part-0-a48b44bb3b6e

If you haven’t checked out my previous tutorial about Setting up Node.js environment on Ubuntu (Part-1) follow the link below -
https://medium.com/@akash_jadhav/setting-up-node-js-environment-on-ubuntu-78c5def83b1f

Note:
1) If you have not setup your node.js environment I would suggest you to view THIS tutorial to get setup with your machine.
2) Follow each step and go through its description to not only make your program running but also to understand it in detail.
3) All commands are to be executed on ‘terminal’. (Ubuntu terminal is used this below tutorial)
4) Link to code files on Github here -
https://github.com/AkashJadhav-github/node-projects

That’s it! So lets begin your journey…

First we will write a simple ‘Hello World!’ program to get you the feel of Node.js.

Step 1) — Create an empty directory at any location, where it is easily accessible to you. Name it ‘node-projects’ and navigate to the empty directory. (or you can name it anything you want)
Suppose, I create my directory at ‘Desktop’ location

cd ~/Desktop (navigate to Desktop location)
mkdir node-projects (to create the new directory)
ls (to view the directory on terminal)
cd node-projects (navigate to the ‘node-projects’ empty directory)

Step 2) — Create a new file there using mouse click or terminal named as ‘firstCode.js’ and open the file in your favourite editor.

touch firstCode.js (create a new file)
gedit firstCode.js
(opens the file in gedit editor)

Step 3) — Now paste the below code into your file and save it.
Don’t get panic by watching the code, I’ll make you understand each and every line, I promise!

var http = require(‘http’);

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/html’});
res.end(‘Hello World!’);
}).listen(8080);

Step 4) — Now its time to run your first nodejs program.. excited! follow along the steps. Go to the terminal and execute the below command

node firstCode.js

hey, whats going on? where is my command prompt? where is the output? I know you are running out of questions..
Your hard work has paid out.. open your browser and open below URL

localhost:8080

and tada! here is the output.

Now, the theory explained below -

  1. The first line var http = require(‘http’); — Imports the http library in our program. Our program uses HTTP method to create the server and to receive and send response to the user.
  2. http.createServer() — Node.js provides its own server, to run the application. It uses HTTP method to create the server. The req & res objects in function are nothing but request from the client (if any) and response to be sent to the client (if any).
  3. Whenever http.createServer() method is called, it calls/executes its internal written functions — res.writeHead() & res.send(). This type of calling is also called as Callback function (will explain in depth in further tutorials)
  4. http.writeHead() method is used to write the HTTP header and http.end() is used to write the body and end the response object.
    (To be more clear — A standard response object has headers and body, headers usually contains status code, authentication key/authorisation parameters etc.. and body has the data, we wish to send back to the calling service/API/function)
  5. Finally, we have .listen(8080) method called at the end of createServerfunction. As the name itself define, this method is use to tell the createServer method to start the server on port 8080 of the host machine. So when application executes (as you have called on browser by running localhost:8080), its runs over port 8080 of your host machine.
    Note — There is no compulsion to use specific port number for node.js application. You can use any port instead of 8080 as per your wish!

Finally, if you like the tutorial and its detailed explanation, do clap and follow the below link to go to the next tutorial!
Thanks for reading & Happy Nodeing!!

Visit the link for next Node.js tutorial -
https://medium.com/@akash_jadhav/simple-node-program-part-3-51e9a12d35bb

--

--