5 Commonly Asked NodeJS Interview Questions

Manuel Martin Jukisz
3 min readJan 17, 2021

On my research on Node JS to change my current backend language to it i found some interesting question that you will be ask in a interview.

Preparation is as important as your knowledge for a successful programming interview. It’ll give you the confidence to attend the interview without the nerves of uncertainty. This is especially true if you are facing a programming interview for the first time in your life.

I don’t have much experience myself so i thought this will be useful to people like me.

When should and shouldn’t you use Node.js?

Node.js is asynchronous, event-driven, non-blocking, and single-threaded. It makes Node a perfect candidate for developing the following types of applications:

  • Realtime applications like chat and live update providing applications.
  • Streaming applications that stream video or other multimedia content to a large audience.
  • Other I/O intensive applications like collaborative platforms.
  • Web backends that follow microservices architecture.

However, Node.js’ unique qualities make it not an ideal choice for some other types of applications. Applications that carry out CPU-intensive tasks like complex mathematical computations do not bode well with Node.js because of its single-threaded execution.

If you want to learn more about this, check out our article Node.js architecture and when to use Node.js in projects .

What does EventEmitter do?

Every object in Node.js capable of emitting events is a member of the EventEmitter class. http module is one such example.

All EventEmitter classes can use the eventEmitter.on() function to attach event listeners to the event. Then, as soon as such an event is caught, its listeners are called one by one synchronously.

const events = require("events");
const eventEmitter = new events.EventEmitter();
const eventListener = function(){
console.log("event triggered");
}
eventEmitter.on("emitted", eventListener);eventEmitter.emit("emitted");

What is Node’s event loop?

Since Node.js is single-threaded, it has to be non-blocking to prevent the thread from spending too long on a task that takes a long time to complete. The event loop is responsible for achieving this non-blocking behavior.

Its job is scheduling the pending tasks using the application thread.

We know that Node uses callbacks to handle the response returned by an asynchronous function when its task is complete. Similar to the event that created the task, the completion of the task also emits an event. Node.js adds these events that require handling to an event queue.

The event loop iterates over events in the event queue and schedules when to execute their associated callback functions.

What are Node Streams?

Streams are pipelines that read or write data from a source and transfer it to a continuous flow destination. There are four types of streams:

  • Readable
  • Writable
  • Duplex (both readable and writable)
  • Transform (A type of duplex stream. Its output is calculated using the input)

Each stream is also an EventEmitter. It means a stream object can emit events when there are no data on the stream, when data is available on the stream, or when data in the stream is flushed from the program.

const fs = require("fs");
const readableStream = fs.createReadStream("test.txt");
let content = "";
readableStream.on("data", (chunk) => {
content += chunk;
});
readableStream.on("end", () => {
console.log(content);
});

What is the difference between readFile and createReadStream functions?

readFile function reads the entire content of the file asynchronously and stores it in the memory before passing it to the user.

createReadStream uses a readable stream that would read the file chunk by chunk without storing the entirety of it into the memory.

createReadStream optimizes the file reading operation compared to readFile by using less memory and making it faster. If the file is of considerable size, the user doesn’t have to wait a long time until it’s entire content is available because small chunks are sent to the user as they are read.

const fs = require("fs");fs.readFile("test.txt", (err, content) => {
console.log( content);
});

--

--