Before starting let us look at some statistics on NodeJs. According to StackOverflow's developers survey, 51.9% of Professional Developers use NodeJs, and according to a survey by nodejs.org about 26% companies in the US and 21% in India use NodeJs for their Backend.
NodeJS has been the most widely used technology for API development in the past few years. I have been a NodeJS developer for 3+ years and have given over 20+ interviews for NodeJS developer roles in many companies. I have also interviewed over 30+ candidates for NodeJs developer roles. In this article, I will list down and briefly explain 5 topics on NodeJs. The most frequent questions that I have faced in an interview or asked a candidate as an interviewer is based on these topics.
NodeJS has been the most widely used technology for API development in the past few years. I have been a NodeJS developer for 3+ years and have given over 20+ interviews for NodeJS developer roles in many companies. I have also interviewed over 30+ candidates for NodeJs developer roles. In this article, I will list down and briefly explain 5 topics on NodeJs. The most frequent questions that I have faced in an interview or asked a candidate as an interviewer is based on these topics.
Now, let's begin
1) NodeJS is single-threaded
So, what is NodeJs? A google search of this will give you some results, and the answer we get is mostly like this:
Node Js is a single-threaded, asynchronous non-blocking I/O model that executes JavaScript on the server-side
NodeJS is single-threaded. What is means is the code we write is executed on a single thread, which is also known as the main thread. Being single-threaded means it has a single call stack and it can do only one thing at a time. But now a question arises - If NodeJS is single-threaded then how does it perform asynchronous non-blocking I/O operations simultaneously. The answer to the question is the Event Loop.
2) NodeJS Event Loop
NodeJS event loop is the heart of the asynchronous nature of NodeJS. The V8 engine runtime, on which NodeJS is built, consists of a heap memory and a call stack, and the NodeJS environment consists of a callback queue, some other APIs, and the Event Loop. All these work together to achieve the asynchronous non-blocking nature of NodeJS. The event loop's main function is to dispatch all the callbacks of various asynchronous functions that have been accumulated in the callback queue. The code execution happens on the main thread while executing when an asynchronous functional call is encountered it is shifted from the main thread and executed separately and when the function completes execution the callback of that function is added to the callback queue and the event loop dispatches it accordingly. To know in detail about how NodeJS works, refer to this article, which we posted on medium.
3) NodeJS Event Emmiter
There are many objects in NodeJS which emit events like fs.readStream emits an event when the specified file is opened. There is a NodeJS module events and all objects which emit events are instances of events.EventEmitter. These objects contain a .on() function by which we can attach an event to it along with the callback for that event. Then using .emit() function,the particular event can be triggered. Have a look at the following code snippet to understand how EventEmitter is used in NodeJS.
4) NodeJS Middlewares
Middlewares in NodeJS are functions that get executed in the middle of the request and response cycle. A middleware has access to the request object, the response object, and to the next middleware function (if any). The code inside a middleware function gets executed before the callback for a particular request is executed. Generally, middlewares are used to change request and response objects, perform authentication, request data verification, etc. To know more about middlewares checkout this link.
5) NodeJS Promises vs Callbacks
This concept is not particularly of NodeJS, it is related to native JavaScript. But still while developing APIs using NodeJS, it is required that one should have a clear knowledge about this. So this topic is common NodeJS interviews also. Now as we all know callbacks are functions that we pass as an argument to another function which is asynchronous in nature. This callback function gets executed when after the parent functions completes its execution. Promises are enhancements to callbacks. For this, we generally write a function that returns a promise object. Promises notify whether the request is fulfilled or rejected. Callbacks can be registered with the .then() to handle fulfillment and .catch() to handle rejection. One problem of callbacks is that nesting of multiple callback functions can lead to something known as callback hell, which decreased code readability, and code management becomes harder. Using Promises, this issue can be solved using promise chaining.
Here we have briefly explained these 5 important topics which are important for a NodeJS interview. Also, it is better if one can get a detailed understanding of these topics. Before we end, a request to anyone who has attended NodeJS interviews in the recent past and has come across important questions, then feel free to comment here, it will be beneficial for others. Also, if you have liked this article do subscribe to get this kind of interview tips directly delivered to your inbox.
0 Comments