WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Basic · question 18 of 100

Explain the concept of ’middleware’ in a Node.js application.?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

Middleware is a crucial concept in Node.js applications, especially when working with frameworks like Express.js. The term "middleware" refers to a set of functions executed in the middle, or between, different stages of processing a request and generating a response in a web application. These functions act as intermediary layers that have access to the request object (req), response object (res), and the next function in the middleware chain, commonly denoted as ‘next‘.

Express middleware is used to handle various tasks like authentication, logging, parsing request data, and other modifications to the request lifecycle. Middleware can be application-level, router-level, or even built-in, like the ones provided by the Express framework itself.

A middleware function’s signature usually looks like this:

function middlewareName(req, res, next) {
  // Middleware implementation goes here
}

When a client sends a request to a Node.js server, it goes through several middleware functions in the order they are defined (registered). Each middleware can either end the request-response cycle or pass the control to the next middleware function using the ‘next()‘ function. If any middleware does not call ‘next()‘ or end the response flow, the request will hang indefinitely.

Here’s an example of using middleware in a simple Express application:

const express = require('express');
const app = express();

// Middleware to log requests
app.use((req, res, next) => {
  console.log('Request received at:', new Date());
  next();
});

// Middleware to add a custom header in the response
app.use((req, res, next) => {
  res.setHeader('X-Custom-Header', 'AwesomeValue');
  next();
});

// Route handlers act as the final middleware in the chain
app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

In the example above, there are two middleware functions, and together with the route handler, they form a middleware chain. The first middleware logs the timestamp of the incoming request, the second one adds a custom header in the response, and finally, the route handler sends the actual response with a "Hello, World!" message. It’s important to note that, for each middleware, we call the ‘next()‘ function to indicate that the processing can proceed further down the chain.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics