Interesting features of Node.js, I learnt
These are the features that make me fascinated about node.js :
Single thread that handles concurrent requests
In simple words it is better to use Asynchronous , non blocking functions in node.js which is single threaded. When we use synchronous , blocking functions, then node server has to wait until the completion of the statement, which not at all good when we are building multi-user server and want to handle multiple requests at a time.
We can learn more about blocking and non blocking in this docs.
So while executing asynchronous and non blocking functions node js can handle other requests as well, whenever it is free in between. This drastically increases the responsiveness and scalability of your server.
Blocking and synchronous functions are helpful when we want single user server and resources can be accessed one by one.
Asynchronous code with Node.js
If we have a statement 1 having async function and statement 2 which is dependent to statement 1, then handling such (or similar wait) scenarios is very important. Otherwise we will fail to execute statement 2 or there will be logical error.
Thanks to async/await and promise in node.js, now we can to wait for statement 1 to be completed using async/await or we can create a promise for statement 1 and then getting resolve or reject state of the promise we will execute the statement 2.
Nested Callback is sometime very complex and can create bad code quality . Async, Await/Async, promise can help to make it better — refer this link.
I came to know about this module when I need to execute set of statement one by one and wanted to pass variables (also wanted to do some manipulation on return object) in each steps. For example : we have list of SQL store procedure and we want to run it one by one in series, also passing the parameters in each store procedure, and writing the `DBMS.PUT_LINE` in some file while executing the sql store procedure. So I followed this oracle-db example and this sample code as the initial step of the implementation .
There are lot of features present which is very helpful to handle set of asynchronous statements. I have used `async.parallel , async.series , async.waterfall` .
All other features are clearly mentioned in documentation.
I learnt how to use express.js module to mostly create REST APIs. It is so popular that when someone is talking about node js REST APIs, it means node.js with express.js framework :D
Core features I came to know are :
- Middleware to respond to HTTP/HTTPS requests — GET, PUT, POST,DELETE, PATCH. It is easy to handle requests using express.js also have eTag support. Errors and validations can be handled in middleware modules.
- Various routing features like getting request paramters, request body, URLs, subrouting is now easy with express.js
Also came to know about :
- How easy to serve files in node server using express.js.
- Static html files as view.
Modular and micro service based architecture
Although I didn’t work on microservice based node server. But I find these links very useful while creating the folder structure and organizing the codebase :
It is easy to configure node server. So it really easy in docker container. When we have complex structure (many databases, tools and micorservices ) then it can be done easily in docker-compose.