Let us simplify “Hello World” Node.js Example and EventLoop

We are learning Node together. In last post We Setup Environment on Windows machine for Node.js Development. We also wrote a Hello World program to verify whether environment is setup as expected or not. In this post we will try to understand each line of Hello World program. Understanding of this is very useful for better grip on complex topics of Node.

Yes when we start learning any new programming language, first program we write to print Hello World. I did the same when started learning Node.

Our Hello World program was as follows,

Server.js

image

Let me try to explain you above program line by line.

Line 1

In this line we are acquiring a package. To work with HTTP request, response and creating servers you need http package loaded into program. You can load that using require. You need http package to perform http operation and create server. Another example could be, if you are working with file system then you will have to load fs package. So as first line of code we loaded http package.

Line 2

In Line 2 we are creating Server. Server is created using createServer function. It takes an anonymous function as parameter. There are two parameter to anonymous function

  1. Server request : http.ServerRequest
  2. Server response : http.ServerResponse

Line 3

In Line 3 we are writing response header. Response header can be created using method writeHead. This takes following parameter

  1. Status code. In this case we have set it as 200.
  2. It takes a JSON object as optional second parameter. In this you can construct other response header information like
  1. Content length
  2. Content Type
  3. Connection
  4. Accept type

So you can construct header with above information as below,

image

Line 4

Line number 4 can be written in different ways. ServerResponse.end indicates the communication has been finished. It takes optional two parameters.

  1. Data in form of either string or buffer
  2. If Data is string then second parameter is encoding. Default encoding is utf8

In this case we are passing string to client in ServerResponse.end . Other way to pass data is using ServerResponse.write. If you use ServerResponse.write() then you will have to explicitly close communication using ServerResponse.end() without any value for parametres.

Line 5

In Line number 5 createServer() method is finished with chained http.Server.listen() method. This method specify

  1. Port number on which incoming request will be heard
  2. Optional host name.

Line 6

Printing message using Console object.

We need to understand that Unlikely other web servers Node works on single process. It handles all the requests ASYNCHRONOUSLY on the same process. It does not create new Process or spawn new Thread for each request. You can say Node is single Process web server with many Asynchronous events. Let us put in step by stop that how Node works. There are two concepts you should know to understand working of Node.

image

Node keep poling for events in Event Loop. When it gets an event it process that asynchronously and assign a Callback to this. You can say Calllback is a function which act as Event Listener and get fired on the complete of an event. Node never waits for an event to complete. It assign event request to a EventListener . EventListener or CallBack get executed once event execution is complete.

To summarize Node always works on Single process and execute each request asynchronously. I hope you find this post useful. Thanks for reading.

3 responses to “Let us simplify “Hello World” Node.js Example and EventLoop”

  1. I just read this blog post – I think one of the best ever!

  2. Dhananjay Kumar

    Thanks Pinal sir 🙂

  3. […] Let us simplify “Hello World” Node.js Example and EventLoop : Part 2 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com