Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

What is test pyramid? Explain with an example?

Answer»

A test pyramid is a metaphor that helps the grouping of software tests in a bunch of different granularity. Unit testing pyramid number of tests GETS easily fitted.

For INSTANCE, unit testing, component testing, integration testing, system testing, end-to-end testing, UI testing, and OTHERS. The primary EXAMPLE of it is creating a unit test which will be the fastest and reliable.

52.

List some events that fired by streams in Node JS?

Answer»
  • newListener
  • removeListener
  • EventEmitter: 'removeListener'
  • EventEmitter. listenerCount (EMITTER, eventName)
  • EventEmitter.defaultMaxListeners
  • emitter.addListener(eventName,LISTENER)
  • emitter.emit(eventName[,...ARGS])
53.

What is a Reactor Pattern in Node.js?

Answer»

Reactor PATTERN is used for non-blocking Input/Output OPERATIONS in the Node.js. This pattern provides a handler that is associated with I/O operations. When I/O requests are generated, they get submitted to a demultiplexer, which handles concurrency in non-blocking I/O mode and collects requests in the form of an event and queues the events.

Note: This Node js interview questions have been created by Node.js Experts. It shall HELP you to answer some of the most frequently ASKED questions during a job interview.

54.

Define an error-first callback?

Answer»

ERROR-first callbacks are generally USED to pass ERRORS as well as data. It is important to pass the error as the first parameter, and then you must check if SOMETHING WENT wrong. There are additional arguments that are used to pass data.

55.

What is the timing features of Node.js? Explain some Timers modules?

Answer»

The timing feature of node.js CONTAINS all the necessary functions that are required to execute code after any specific period. As it is built on the V8 JavaScript ENGINE of Google Chrome, hence its library permits the FAST execution of code. Timers also provide a number of ways for managing schedules. They don't need to be imported as all the METHODS are easily available for the browser.

Some modules are –
  • setTimeout
  • setInterval
  • setImmediate
  • clearImmediate(IMMEDIATE)
  • clearTimeout(timeout)
56.

What are the difference between setTimeout() and clearTimeout()?

Answer»

setTimeout() : It can be used to schedule CODE EXECUTION after a designated amount of milliseconds.

function myFunc(arg) {
  console.log(`arg was => ${arg}`);
}
setTimeout(myFunc, 1500, 'funky');

clearTimeout()  :  It can be used to CANCEL timeout which are set by setTimeout().

console.log('before IMMEDIATE');
setImmediate((arg) => {
   console.log(`executing immediate: ${arg}`);
}, 'so immediate');
console.log('after immediate');

57.

How we can open a file in Node JS?

Answer»

OPEN() FUNCTION is used to opens a file by passing a file name.

Syntax : fs.open(path, flags[mode], callback)

Parameters

  • path : It is a string having file name with complete path
  • flags : It indicates the behavior of the file to be opened
  • mode : It sets the file mode like permission
  • callback : gets two arguments (err, FD)
Example

var fs = require("fs");

// ASYNCHRONOUS - Open a File
console.log("open file");
fs.open('file.txt', 'r+', function(err, fd) {
   if (err) {
      return console.error(err);
   }
   console.log("File opened");     
});

58.

What is Piping in Node JS?

Answer»

A pipe is a function that reads the data from a readable STREAM once it is available and writes it in the destination WRITABLE stream. It performs all the REASONABLE things including the errors, files in CASE any one side falls apart. In due case, piping redirections any readable stream to a writable stream.

59.

What is a buffer and stream in Node.js?

Answer»

Buffer

It is a global class that can be accessed in an application without importing the buffer MODULE. It can be constructed in a variety of ways like VAR buf = new Buffer(5);

Use of a buffer in Node JS

Buffer is used to dealing with TCP streams, reading, and writing data to the filesystem in the Node.js server, which are PURELY binary teams of data.

It is designed in a way that is easy to manage, faster and has an API designed for binary data, which makes it the perfect choice for handling streams of data.

Stream

these are the objects that allow the developers to read the data from the source or can write the data to the destination in an endless manner. They usually have problems with the large data. With a stream, there is no need to wait for the whole resource to load.

There are DIFFERENT types of Stream:

  • Readable
  • Writable
  • Duplex
  • Transfer
60.

How do you update NPM to a new version in Node.js?

Answer» EXAMPLE

$ sudo npm install npm -G
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
[EMAIL protected] /usr/lib/node_modules/npm

61.

How we can secure HTTP cookies against XSS attacks?

Answer»

we have to set flags on the set-cookie HTTP header:

  • HttpOnly
  • secure

After this it will lokk like this : Set-Cookie: sid=; HttpOnly

Point to be noted: Make SURE that you GO through this twice as this is the FAVORITE node js interview QUESTIONS for fresher and experienced as WELL.

62.

What do you understand by middleware? How can you use middleware in Node JS?

Answer»

Middleware functions are those functions having access to the request object (req), the response object (RES), and also the next FUNCTION in an application's request-response cycle. The following function is BASICALLY a function in the Express router, when invoked, executes the middleware which is succeeding in the current middleware.

Middlewares can perform the following TASKS:
  • Execute any type of code
  • Making changes to the request and response objects
  • We are ending the request-response cycle.
  • Cleaning the next middleware in the STACK
63.

What is chaining in Node.js?

Answer»

This is a MECHANISM by which the output of one stream is CONNECTED to the output of another stream y creating a long chain of MULTIPLE stream OPERATIONS.

64.

How to get a user's IP address in node js?

Answer»

You can USE req.connection.remoteAddress to a USER's IP ADDRESS in node js.

65.

How will you debug an application in Node.js?

Answer»

The easiest way to debug an application on Node.js is through Node-Inspector. You can use it from any BROWSER SUPPORTING WebSockets. It helps multiple WAYS of debugging LIKE a profiler, livecoding, breakpoints, etc. Here's how to use node-inspector:

  • Install it using npm install -G node-inspector
  • Now, run node-debug app.js
66.

What are the different HTTP methods in Node.js?

Answer»

There are three HTTP methods in NodeJS. They are:

  • http.createServer(): This is used to create a new instance of the http.SERVER class.
  • http.REQUEST(): This is used for making an HTTP request to the server by creating an instance of the http.ClientRequest class.
  • http.get(): It is very much similar to the http.request() method, but it automatically SETS the HTTP method to GET and calls the req.end().
Also READ: What's New for Node.js in 2020
67.

Why are promises better than callbacks?

Answer»

Here are some of the reasons why promises are better than callbacks:

  • They are built over callbacks and very efficient abstractions.
  • They allow cleaner and better functional code.
  • They end up with FEWER error-prone boilerplate.
  • They provide a catching mechanism that is not available in Callbacks.
  • You have a SUPERIOR level of CONTROL and trust while delegating tasks through promises RATHER than callbacks.
68.

How is observable different from promise?

Answer»
ObservablesPromises
These are used for streams of events over time.It is able to handle only one event.
It SUPPORTS operators such as map, filter, reduces, etc., and is cancel-able and retry-able.It is not easy to handle and returns a SINGLE value.
It provides chaining and subscriptions for handling complex applications.It uses only the .then() clause.
Here the subscribe method is used for a centralized and predictable error handling.It pushes the errors to the child's promises.

Note: This node js INTERVIEW questions have been created by seasoned Node.js experts. It SHALL help you to answer some of the most frequently asked questions during a job interview.