Introduction to NodeJS
It is a popular tool for building backend services like Api’s, data-intensive, real-time and almost any kind of app.
But here is the question how we will perform the I/O operations like reading from the network, accessing the database or filesystem on our server?
Well here the magic happens every blocking operation will the goes into the C++ internal thread pool. Where when operation is completed, it will come to the job queue and resume the operation via callback. This allows the NodeJS to handle the thousands of concurrent connections with a single server without any burden of managing the thread concurrency.
By using the NodeJS, now thousands of frontend developers can write the code for server side as well without any different language.
Now let’s build a hello world application in NodeJS web server.
In the above code we had used the ‘http’ module in NodeJS.
We had created the server listening on ‘127.0.0.1’ address by using the createServer() method of http module.
When a new request received the, request event is called, this event provide the two objects: a request and a response.
These two objects are essential for handling the HTTP call.
The request object provides the request details and the response is used to return the data to the caller.
In our case we didn’t use the request object here. But we had used the response object with setting the statusCode, setHeader and end metods.
NodeJS is a runtime environment to run the JS other then browser.
It’s single thread and handles thousands of concurrent requests without adding any new thread.
NodeJS has vast number of libraries and very useful essential modules to build applications.