Node.js Interview Questions

nodejslogo
nodejslogo

Node.js is a server-side technology that’s based on Google’s V8 JavaScript engine.It has become quite popular in recent times and node.js developers are quite in demand due to popularity of MEAN(Mongo,Express.js,AngularJS,Node.js) stack.
This post helps you to know some popular Node.js Interview Questions that you need to know.

Node.js Popular Questions:

1)What is event loop in Node.js?
Ans:Node js is a single threaded application but it support concurrency via concept of event and callbacks.Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the event listener function to get executed.
Basically nodejs internally has a callstack, callback queue and an event loop to pass events from callback queue to call stack once call stack is empty.

eventloopnodejs
eventloopnodejs

2)How to handle uncaught exceptions in Node.js?
Ans:We can easily handle uncaught exceptions in Node.js using domains. By using domains we can fire an error event for a particular domain and use different exception handlers for different scenarios. This allows us to handle exceptions depending on where they occur.

This is especially useful when we need to move Node.js app to production.

3)What is piping in Node.js?
Piping is the process of connecting output of one stream to input of another stream.

4)In which of the following use-cases is use of Node.js favorable:
a)A CPU intensive task like fining a transpose of 1000×1000 matrix
b)An application which will get data from backend databases and pass it to frontend in the form of REST Apis
Ans:
Nodejs is not favorable to use in the first case since the CPU intensive operation will block Node.js responsiveness.
It is favorable to use in second case since it can synchronously handle requests from frontend and pass data to frontend application in the form of REST Apis using its event-driven model.

5)What is callback hell and how to avoid it in Node.js?
Ans:Callback hell occurs when we try to nest multiple callbacks one after another until our code becomes a mess that we ourselves cannot figure out.
The most easiest way to prevent this situation is to use async.js to handle execution of multiple callback functions.
Many helper methods exist in Async that can be used in different situations, like series, parallel, waterfall, etc. Each function has a specific use-case.

Another solution is to use promises and modularize the code to have proper control flow in code.

Image source:Stackoverflow

Share post-
About Saurabh Mhatre 70 Articles
Currently working in web and hybrid application development