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,
Let me try to explain you above program line by line.
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.
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
- Server request : http.ServerRequest
- Server response : http.ServerResponse
In Line 3 we are writing response header. Response header can be created using method writeHead. This takes following parameter
- Status code. In this case we have set it as 200.
- It takes a JSON object as optional second parameter. In this you can construct other response header information like
- Content length
- Content Type
- Accept type
So you can construct header with above information as below,
Line number 4 can be written in different ways. ServerResponse.end indicates the communication has been finished. It takes optional two parameters.
- Data in form of either string or buffer
- 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.
In Line number 5 createServer() method is finished with chained http.Server.listen() method. This method specify
- Port number on which incoming request will be heard
- Optional host name.
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.
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.