DEV Community

Cover image for NodeJS Basics Interview Questions
Aditya Yadav
Aditya Yadav

Posted on

NodeJS Basics Interview Questions

What is Node.js?

Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser.

What is it used for?

Node.js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. Node.js is used to build different type of applications such as web applications, real-time chat applications, REST API servers etc.

What is the advantage of using node.js?

  • It provides an easy way to build scalable network programs
  • Generally fast
  • Great concurrency
  • Asynchronous everything
  • Almost never blocks

Why Node.js is single threaded?

Node.js uses a single threaded model in order to support async processing. With async processing, an application can perform better and is more scalable under web loads.

How many types of API functions are there in Node.js?

There are two types of API functions in Node.js:

  • Asynchronous, non-blocking functions
  • Synchronous, blocking functions

Define Asynchronous and Non-blocking functions ?

Node.js based server never waits for an API to return data thus making it asynchronous.

Non-blocking functions are used in regards with I/O operations. They immediately respond with whatever data is available and keeps on running as per the requests. In case, any answer couldn’t be retrieved then the API returns immediately with an error.

What is Event-driven programming?

Event-driven programming is building our application based on and respond to events. When an event occurs, like click or keypress, we are running a callback function which is registered to the element for that event.

What is an Event-loop?

An event loop in Node.js handles all the asynchronous callbacks in an application. Node.js (or JavaScript) is a single-threaded, event-driven language. This means that we can attach listeners to events, and when a said event fires, the listener executes the callback we provided.

How does Event-loop work?

Whenever functions like setTimeout, http.get, and fs.readFile are called, Node.js executed the event loop and then proceeds with the further code without waiting for the output. Once the entire operation is finished, Node.js receives the output and then executes the callback function. This is why all the callback functions are placed in a queue in a loop. Once the response is received, they are executed one by one.

alt text

What is callback functions?

A callback is a function called at the completion of a given task, this prevent any blocking, and allows other code to be run in the meantime.

function A(callback){
  B(function (err,data){
    if(err){
      console.log("Error occored");
      callback(err)
    }
    data+=1;
    callback(data);
  });
}
Enter fullscreen mode Exit fullscreen mode

Explain REPL in the context of Node.js.

REPL in Node.js stands for Read, Eval, Print, and Loop. It represents a computer environment such as a window console or Unix/Linux shell where any command can be entered and then the system can respond with an output. Node.js comes bundled with a REPL environment by default.

Explain the purpose of module.exports?

A module encapsulates related code into a single unit of code. This can be interpreted as moving all related functions into a file.

module.exports = {
A: function(){
return "NAMASTE";
},
B: function(){
return "Hello";
}};
Enter fullscreen mode Exit fullscreen mode

What is callback hell?

Callback hell is heavily nested callback which makes the code unreadable and difficult to maintain.

Explain the concept of middleware in Node.js?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.

What are the different types of HTTP requests?

HTTP defines a set of request methods used to perform desired actions. The request methods include:

GET: Used to retrieve the data

POST: Generally used to make a change in state or reactions on the server

HEAD: Similar to the GET method, but asks for the response without the response body

DELETE: Used to delete the predetermined resource

Top comments (0)