Node JS Interview Questions and Answers

What is Node Js ?

Node.js is a very powerful JavaScript based platform or framework which is built on Google Chrome's JavaScript V8 Engine.

What are the features of Node.js?

Here is the main features and advantages of node js is,

  • Asynchronous and Event Driven
  • Very Fast
  • Single Threaded but Handling multiple threads at a time.
  • No Buffering

 

Callbacks in Node Js.

Callback is a asynchronous function. It will execute in asynchronous manner. In node js heavily used callback functions for non blocking. And also execute parallel.

Callback function example : 

function sum(a,b,callback){
    callback(a+b);
}

sum(2,3,function(result){
    console.log(result);
});

 

 

 

What is callback hell in Node.js?

Callback hell is nothing but writing heavily nested Callback functions. It is very hard to maintain, unreadable and error handling also somewhat difficult.
Example :
function sum(a,b,callback){
    callback(a+b);
}
 
sum(2,3,function(result){
	console.log(result);
	sum(6,9,function(result1){
		console.log(result1);
		sum(12,18,function(result2){
			console.log(result2);
			sum(24,36,function(result3){
				console.log(result3);
				sum(48,72,function(result3){
					console.log(result3);
				});
			});
		});
	});
});

How to avoid Callback Hell in Node.js

By using Promises we can avoid callback hell
Example for Promises

function sum(a,b){
    return new Promise(function(resolve, reject) {
        if(isNaN(a) || isNaN(b)){
            reject("values must be integers");
        }else{
            resolve(parseInt(a)+parseInt(b));
        }
    });
}

sum(2,'3')
.then(function(result){console.log(result);})
.catch(function(error){console.log(error);});

Here success response will get in then function, failure response will get in catch function

Below are the some modules for avoiding callback hell.
promise, async, q, deferred

when to use Node.js?

Node.js can be used for the following type of applications

a) Web applications
b) Light Weight applications (because of single threaded)
c) Distributed systems
d) Gaming applications
e) Chat applications

How to access DOM using Node.js?

No we can't  access DOM from the node, because node don't have DOM/window objects.

What is the difference between Node.js vs Ajax?

The only similarity is both are javascript. But both are doing different works.

Ajax is client side technology, without reaload the page we can do some operations are loading content and saving information. Comming to Node js is server side technology. Any db related works we can do with node js and Node.js does not work in the browser. 

 

Which framework most used in node.js?

Express is the most used framework in node.js because Fast and flexible for all different kinds of requirements. And also Express is the part of MEAN stack

List of some Node.js Global Objects


Global Objects nothing but no need include in your application, we can directly access and use them.

Below are the some global objects
__dirname
__filename
process
buffer

In general
console
timeout functions like setTimeout/setInterval/setImmediate

 

How node.js works?

Node.js works on a v8 environment, it is a virtual machine that utilizes JavaScript as its scripting language and achieves high output via non-blocking I/O and single threaded event loop.

What are the main disadvantages of using Node.js?

Some of the disadvantages are:

Unstable API and Inconsistencies: One of the biggest disadvantages of Node.js is that it lacks consistency. Node.js’ API changes frequently, and the changes are often backward-incompatible.
Not Suitable for Heavy-Computing Apps: Node.js doesn’t support multi-threaded programming yet. It is able to serve way more complicated applications than Ruby, but it’s not suitable for performing long-running calculations.
Event Driven Model: Event driven model will confuse a lot of programmers who are new to JavaScript. The callback chain can get very long which makes it harder to maintain.

What do you mean by the term I/O ?

I/O is the shorthand for input and output, and it will access anything outside of your application. It will be loaded into the machine memory to run the program, once the application is started.

What does event-driven programming mean?

In computer programming, event driven programming is a programming paradigm in which the flow of the program is determined by events like messages from other programs or threads. It is an application architecture technique divided into two sections

1) Event Selection
2) Event Handling

function addToCart(productId){
    event.send("cart.add", {id: productId});
}

event.on("cart.add", function(event){
    show("Adding product " + event.id);
});

 

What are the two types of API functions in Node.js ?

The two types of API functions in Node.js are
a) Asynchronous, non-blocking functions
b) Synchronous, blocking functions

What is control flow function?

A generic piece of code which runs in between several asynchronous function calls is known as control flow function.

Explain the steps how "Control Flow" controls the functions calls?

a) Control the order of execution
b) Collect data
c) Limit concurrency
d) Call the next step in program

Why Node.js is single threaded?

For async processing, Node.js was created explicitly as an experiment. It is believed that more performance and scalability can be achieved by doing async processing on a single thread under typical web loads than the typical thread based implementation.

Does node run on windows?

Yes – it does. Download the MSI installer from http://nodejs.org/download/

Using the event loop what are the tasks that should be done asynchronously?

a) I/O operations
b) Heavy computation
c) Anything requiring blocking

Why node.js is quickly gaining attention from JAVA programmers?

Node.js is quickly gaining attention as it is a loop based server for JavaScript. Node.js gives user the ability to write the JavaScript on the server, which has access to things like HTTP stack, file I/O, TCP and databases.

What are the two arguments that async.queue takes?

The two arguments that async.queue takes
a) Task function
b) Concurrency value

What is an event loop in Node.js?

To process and handle external events and to convert them into callback invocations an event loop is used. So, at I/O calls, node.js can switch from one request to another .

Mention the steps by which you can async in Node.js?

By following steps you can async Node.js
a) First class functions
b) Function composition
c) Callback Counters
d) Event loops

What are the pros and cons of Node.js?

Pros:
a) If your application does not have any CPU intensive computation, you can build it in Javascript top to bottom, even down to the database level if you use JSON storage object DB like MongoDB.
b) Crawlers receive a full-rendered HTML response, which is far more SEO friendly rather than a single page application or a websockets app run on top of Node.js.
Cons:
a) Any intensive CPU computation will block node.js responsiveness, so a threaded platform is a better approach.
b) Using relational database with Node.js is considered less favourable