Tuesday, 28 January 2020

Top 20 Node.js Interview Questions

               Preparing for the Node js interview questions, you should start with the basic concepts to advanced topics and move up from down. It is important to understand how things work before moving on the more complex topics. To get the best understanding of Node.js, you must have a basic understanding of JavaScript related concepts as well.

 
Ques: 1. What can be the possible use of DNS module in Node.js?

Ans. This is one of the most asked Node js interview questions. The DNS module consists of an asynchronous network wrapper. Let’s have a look at the most commonly used functions of this module:

1. DNS.lookup(address, options, callback) – This method takes any website’s address as its first parameter and returns the corresponding first IPV4 or IPV6 record. The options parameter can be an integer or object. If no options are provided both IPV4 and IPV6 are valid inputs. The third parameter is the callback functions.

2. DNS.lookupservice(address, port, callback) – This function converts any physical address to an array of record types. The record types are specified by the second parameter, rrbyte. The third method is the callback function.

3. dns.getServers() – This function returns an array of IP address strings that are currently configured for DNS resolution, formatted according to rfc5952. A string will include a port section if a custom port is used.

4. DNS.setServers() – This function sets the IP address and port of servers to be used when performing DNS resolution. The DNS.setServers() method must not be called while a DNS query is in progress.


Ques: 2. How can you use Timers in Node.js?

Ans. As the name suggests, the Timer module executes code after a set period of time. It doesn’t need to be imported via require(). All the methods are available globally to emulate the browser JavaScript API which provides several ways of scheduling code to execute at a certain time.
The functions provided by Node.js for Timers are:

setTimeout(), setImmediate(), and setInterval.


Ques: 3. What do you mean by non-blocking mean in Node.js?

Ans. If you are talking about non-blocking in Node.js, you are much aware about the non-blocking I/O.  It can be explained how Node uses libuv to handle its IO in a platform-agnostic way. A non-blocking request is made and upon a request, it queues it within the event loop. The JavaScript callback is then called on the main JavaScript thread.


Ques: 4. What is the main difference between Node.js and Ajax?

Ans. The easiest way to explain the difference between Node.js and Ajax is that Node is a server-side JavaScript, while Ajax is a client-side technology. What that means is that Ajax is often used for updating the contents of the page without refreshing it. Node, on the other hand, is used for developing server software, executed by the server rather than in the browser.


Ques: 5. What are the various pros and cons of Node.js?

Ans. Let’s take a look at the most important pros and cons:

Pros: 

  • In the cases when your app doesn’t have any CPU intensive computation, you can build the whole thing in Javascript, including the basic database level. All you have to do is use JSON storage object DB like MongoDB. 
  • Crawlers receive a full-rendered HTML response which is great for SEO.

Cons: 

  • Node.js responsiveness is blocked by an intensive CPU computation so a threaded platform would a better approach in those cases. 
  • Using a relational database with Node.js is considered less favourable.


Ques: 6. What is the use of method spawn() and fork()?

Ans. The spawn method is used when a new process is to be launched with a given set of commands. Check out the following command:
child_process.spawn(command[, args][, options])

The fork method is considered to be a special case for spawn() method. The following code shows how to use it:
child_process.fork(modulePath[, args][, options])


Ques: 7. Can you explain the EventEmitter in Node.js?

Ans. EventMitter class for event module helps with raising and handling custom events. You can access it with the following code:

// Import events module
var events = require(‘events’);
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();


Ques: 8. What do you understand by tracing?

Ans. The purpose of tracing is to trace information generated by V8. To enable it, pass flag-trace-events-enabled when starting the node.
All recorded categories can be specified by the flag–trace-event categories. The enabled logs can be opened as chrome://tracing in Chrome.


Ques: 9. Can you explain the control flow function and steps to execute it?

Ans. The control flow function is the code that runs between asynchronous function calls. Following steps should be followed to execute it: 

  1. Control the order of execution. 
  2. Collect data. 
  3. Limit concurrency. 
  4. Call the next step in the program.


Ques: 10. What is the file system module of Node js?

Ans. The file system module performs a file-related operation. It comprises synchronous and asynchronous functions to read/write files.

For instance, readFile() function is asynchronous function to read file content from specified path and readFileSync() is synchronous function to read files.


Ques: 11. What are some of the most popular modules of Node.js?

Ans. The most popular modules Nodes.js are:

  • xpress 
  • async 
  • browserify 
  • socket.io 
  • bower 
  • gulp 
  • grunt


Ques: 12. What do you understand by package.json in node.js? What is it used for?

Ans. Package.json holds various metadata information about the project. The information contained in this file is then given to npm to identify the project and to handle its dependencies.  
Some of the fields are name, description, author, and dependencies.
The dependencies are then installed whenever the project is installed through npm. Moreover, if the npm install is run in the root directory of the project, the dependencies will be installed in ./node_modules directory.


Ques: 13. What is the difference between Asynchronous and Non-blocking?

Ans. Asynchronous or simply not synchronous means that the HTTP requests are not waiting for the server response. You can continue with other block and respond to the server response when received.
Non-Blocking, on the other hand, is a term commonly used with IO. For instance, non-blocking read/write calls return and expect the caller to call again. Read will wait until it has some data and put calling thread to sleep.


Ques: 14. What are the features of Node.js?

Ans. Since the APIs of Node.js library are asynchronous, they are non-blocking which means that a Node.js based server never waits for an API to return data. Node.js is a system built on Google Chrome’s V8 JavaScript Engine which makes it very fast.

This system uses a single threaded model with event looping. With the event mechanism helping the server respond in a non-blocking way, the server is highly scalable compared to the other servers that create limited threads to handle requests.


Ques: 15. Why should you use Node.js?

Ans. One could use Node.js when they want to easily build scalable apps. The biggest pros include:

  • It is very fast in code execution.
  • It is asynchronous and event-driven.
  • It is single-threaded but highly scalable.
  • No buffering.


Ques: 16. Explain the role of REPL in Node.js.?

Ans. REPL stands for Read, Evaluate, Print, Loop – and it performs all the mentioned tasks accordingly. It is used to execute ad-hoc JavaScript statements and it is a very important part of the testing and debugging process.


Ques: 17. What is libuv library? And what is its use in Node.js?

Ans. Libuv is a multi-platform support library with a focus on asynchronous I/O. Although it was primarily developed for Node.js, it’s also commonly used by other systems such as Luvit, Julia, pyuv etc.
Back in the days when the whole Node project started, it was using Google’s V8 and Marc Lehmann’s libev. However, the problem with libev was that it ran only on Unix so they had to come up with the new solution to make it work on Windows, especially once node.js grew in popularity.
Libuv was an abstraction around libev or IOCP depending on the platform, providing users an API based on libev. In the node-v0.9.0 version of libuv, libev was removed.
Here are some of the key features:

  • Full-featured event loop backed by epoll, kqueue, IOCP, event ports. 
  • Asynchronous TCP and UDP sockets. 
  • Asynchronous file and file system operations.
  • Child processes.
  • File system events.


Ques: 18. What are the two arguments that async.queue takes?

Ans. These two arguments are:

a)      Task function

b)      Concurrency value


Ques: 19. How to avoid Callback Hell?

A. Basically, every time a long-running query finishes its execution, the callback associated with the query is run. Since Node.js uses single thread only, it happens that it leads to numerous queued events. And that is where the callback comes in.
There are 4 common solutions for this issue:

Modular code – the code split into smaller modules that are later joined together to the main module again

Promise mechanism – ensures either a result or error; it’s an alternative way for the async code and it takes two optional arguments, one of which is called depending on the state of promise

Use of generators – they wait and resume using the yield keyword but can also suspend and resume async operations.

Async mechanism – the module with <async.waterfall> API which passes data from one operation to another using the next callback.


Ques: 20. What is NPM?

A. NPM stands for Node Package Manager. It has 2 important functions:

It works on Online Repository for node.ls packages which are present at <nodejs.org>. In addition to that, it also works as a command line utility and does version management.

You can verify version using below command: npm –version.

To install any module you can use: npm install <Module Name>

No comments:

Post a comment