NodeJS Interview Questions and Answers
Question - 21 : - Why does Google use the V8 engine for Node.js?
Answer - 21 : - Google makes use of the V8 engine because it can easily convert JavaScript into a low-level language. This is done to provide high performance during the execution of an application and also to provide users with real-time abilities to work with the application.
Question - 22 : - What is the difference between spawn and fork methods in Node.js?
Answer - 22 : - The spawn() function is used to create a new process and launch it using the command line. What it does is that it creates a node module on the processor. Node.js invokes this method when the child processes return data.
The following is the syntax for the spawn() method:
child_process.spawn(command[, args][, options])
Coming to the fork() method, it can be considered as an instance of the already existing spawn() method. Spawning ensures that there are more than one active worker node to handle tasks at any given point of time.
The following is the syntax for the fork() method:
child_process.fork(modulePath[, args][, options])
Question - 23 : - What is the use of middleware in Node.js?
Answer - 23 : - A middleware is a simple function that has the ability to handle incoming requests and outbound response objects. Middleware is used primarily for the following tasks:
• Execution of code (of any type)
• Updation of request and response objects
• Completion of request–response iterations
• Calling the next middleware
Question - 24 : - What are global objects in Node.js?
Answer - 24 : - Global objects are objects with a scope that is accessible across all of the modules of the Node.js application. There will not be any need to include the objects in every module. One of the objects is declared as global. So, this is done to provide any functions, strings, or objects access across the application.
Next among the Node js coding questions you need to take a look at the usage of assets in Node js.
Question - 25 : - Why is assert used in Node.js?
Answer - 25 : - Assert is used to explicitly write test cases to verify the working of a piece of code. The following code snippet denotes the usage of assert:
var assert = require('assert');
function add(x, y) {
return x + y;
}
var result = add(3,5);
assert( result === 8, 'three summed with five is eight');
Question - 26 : - What are stubs in Node.js?
Answer - 26 : - Stubs are simply functions that are used to assess and analyze individual component behavior. When running test cases, stubs are useful in providing the details of the functions executed.
Question - 27 : - How is a test pyramid implemented using the HTML API in Node.js?
Answer - 27 : - Test pyramids are implemented by defining the HTML API. This is done using the following:
• A higher number of unit test cases
• A smaller number of integration test methods
• A fewer number of HTTP endpoint test cases
Question - 28 : - Why is a buffer class used in Node.js?
Answer - 28 : - A buffer class is primarily used as a way to store data in Node.js. This can be considered as a similar implementation of arrays or lists. Here, the class refers to a raw memory location that is not present in the V8 heap structure.
The buffer class is global, thereby extending its usage across all the modules of an application.
Question - 29 : - Why is ExpressJS used?
Answer - 29 : - ExpressJS is a widely used framework built using Node.js. Express.js uses a management point that controls the flow of data between servers and server-side applications.
Being lightweight and flexible, Express.js provides users with lots of features used to design mobile applications.
Question - 30 : - What is the use of the connect module in Node.js?
Answer - 30 : - The connect module in Node.js is used to provide communication between Node.js and the HTTP module. This also provides easy integration with Express.js, using the middleware modules