eInterviewQuestions

NodeJS Interviews Questions

All our users, you can also add/post your question and answer which is mostly asked by an Interviewer in Interviews, you all can suggest for any changes to make our website best & user friendly. please feel free to Post Question.

Q1. What is Node.js, its features and why to use it?

Node.js was developed by Ryan Dahl and other developers working at Joyent. First rleased on 2009 for Liunix OS.

Node.js is a web application framework built on Google V8 Engine. Node.js allows you to run JavaScript on the server. Node.js is an open source server environment. It is used to build fast and scalable network applications as well as data-intensive real-time web applications.

Featurs

Node.js is a single-threaded but highly scalable system that utilizes JavaScript as its scripting language. It uses asynchronous, event-driven I/O instead of separate processes or threads. It is able to achieve high output via single-threaded event loop and non-blocking I/O.

Use

1. It is generally fast. 

2. It almost never blocks. 

3. Unified programming language for both front-end and back-end

4. Everything is asynchronous 

5. It yields great concurrency.

6. JavaScript with V8 engine performs faster than Php, Ruby, Python, JSP and ASP.NET.


Q2. What is V8 Javascript Engine?

V8 is written in C++ language and implements ECMA Script (ES5). V8 is used in Google Chrome and can be run standalone or can be embedded into any C++ application.

1. V8 uses just-in-time compilation (JIT) to execute javascript code. 

2. It compiles JavaScript to native machine code.

3. It is used by many open source projects like Node.js and MongoDB to execute JavaScript on server side. Also used in MEAN stack developer.


Q3. IDEs for Node.js development?

We can use below IDE forNode JS.

1. Visual Studio 2013, 2015, 2017 or higher

2. Visual Studio Code

3. Node Eclipse


Q4. Few application where we can use Node js.

1. E-Commerce Web Applications

2. Network Applications

3. Proxy Server

4. Social Media Applications

5. Real-time Services or data Applications like Games, Stock Trading.

6. File Uploading Tools

7. HTTP Web Server


Q5. Which operating systems does Node.js supports?

Node.js supports following platforms:

1. Linux

2. Windows

3. Mac OS X.


Q6. Where you can deploy Node application?

Node.js app cannot be deployed on your existing hosts like shared web hosting etc. You can use VPS or dedicated servers to install node and run your application. The easiest way to deploy your node application is to use a scalable service like Heroku.


Q7. Why is Node.js Single-threaded?

Node.js is single-threaded for async processing. By doing async processing on a single-thread under typical web loads, more performance and scalability can be achieved as opposed to the typical thread-based implementation.


Q8. What is callback in Node.js and What is callback hell?

A callback function is called at the completion of a given task. This allows other code to be run in the meantime and prevents any blocking.  Being an asynchronous platform, Node.js heavily relies on callback. All APIs of Node are written to support callbacks. 

Callback hell is the result of heavily nested callbacks that make the code not only unreadable but also difficult to maintain. For example

doSomething(param1, param2, function(err, paramx){

    doMore(paramx, function(err, result){

        insertRow(result, function(err){

            yetAnotherOperation(someparameter, function(s){

                somethingElse(function(x){

                });

            });

        });

    });

});


Q9. What are the limitations of Node.js?

There are following limitations of Node.js:

1. It doesn’t support very high computational intensive tasks. When it executes long running task, it will queue all the incoming requests to wait for execution, since it follows JavaScript event loop which is single threaded. It doesn’t support multi-threaded programming.

2. Node JS is not good for executing synchronous and CPU intensive tasks.


Q10. How do you prevent/fix callback hell?

Ways to prevent/fix callback hell are:

1. Handle every single error

2. Keep your code shallow

3. Modularize – split the callbacks into smaller, independent functions that can be called with some parameters then joining them to achieve desired results.


Q11. Explain Blocking and non-blocking operations?

Blocking methods execute synchronously and non-blocking methods execute asynchronously.

Blocking Code: The given code is a blocking code, since it will not continue the execution of the next lines of code until the read operation is completed. 

Synchronous file read example.

const fs = require('fs');

const data = fs.readFileSync('/file.md'); // blocks here until file is read

Un-blocking Code: The above blocking code can be converted into non-blocking code with the help of callback function. Hence, the given code will continue the execution of the next lines of code without waiting for read operation to be complete. var fs = require('fs');

Equivalent asynchronous file read example:

const fs = require('fs');

fs.readFile('/file.md', (err, data) => { //doesn’t wait and will read file asynchronously 

  if (err) throw err;

});