Node.js interview questions set 2


Node.js interview questions set 2
Let start with set 2,

5)What is buffer in node.js?
Ans: Node.js includes an additional data type called Buffer (not available in browser's JavaScript). 
Buffer is mainly used to store binary data while reading from a file or receiving packets over the network.
Buffer class is a global class that can be accessed in an application without importing the buffer module.

Ex:var buf = new Buffer(10);

6)Explain the concept of URL module in Node.js?

Ans: The URL module in Node.js splits up a web address into readable parts. 
Use require() to include the module:
var url = require('url');
Then parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties.

var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2021&month=september';
var q = url.parse(adr, true);

console.log(; //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(; //returns '?year=2021&month=september'

var qdata = q.query; //returns an object: { year: 2021, month: 'september' }
console.log(qdata.month); //returns 'september'

7)Is Node.js a single thread? 
Ans: Yes, it is true that Node.js processes all requests on a single thread. But it is just a part of the theory behind Node.js design. In fact, more than the single thread mechanism, it makes use of events and callbacks to handle a large no. of requests asynchronously.

Moreover, Node.js has an optimized design that utilizes both JavaScript and C++ to guarantee maximum performance. JavaScript executes at the server-side by the Google Chrome v8 engine. And the C++ lib UV library takes care of the non-sequential I/O via background workers.

To explain it practically, let’s assume there are 100s of requests lined up in the Node.js queue. As per the design, the main thread of the Node.js event loop will receive all of them and forward them to background workers for execution. Once the workers finish processing requests, the registered callbacks get notified on the event loop thread to pass the result back to the user.

8)Explain callback hell in Node.js?
Ans: Callback hell is a phenomenon that afflicts a JavaScript developer when he tries to execute multiple asynchronous operations one after the other.

An asynchronous function is one where some external activity must complete before a result can be processed; it is “asynchronous” in the sense that there is an unpredictable amount of time before a result becomes available. Such functions require a callback function to handle errors and process the result.

How to avoid callback hell?
    getMoreData1(a, function(b){
        getMoreData2(b, function(c){ 
            getMoreData3(c, function(d){ 
	            getMoreData4(d, function(e){ 

Using Async.js
Using Promises
Using Async-Await


Popular posts from this blog

Node JS:Understanding bin in package.json.

Node.js: create an excel file with multiple tabs.

Node.js: Extract text from image using Tesseract.