Home » Blog » Coding Interview Prep – Intro to Express.js & Middleware

Coding Interview Prep – Intro to Express.js & Middleware

While preparing for a coding interview, I found myself feeling a bit rusty on theoretical questions and scenarios I haven’t dealt with recently in my everyday work. After all, a developer doesn’t have to remember everything, right? Based on experience, a developer usually just remembers something exists — or has to look it up if they’ve never built anything similar.

So what’s this post about? This — and others coming soon — will be part of a series covering questions I believe are important for any developer to know in order to feel confident going into an interview focused on Express.js. If you can answer these questions, you should feel pretty good. You won’t know everything, but you’ll be in solid shape to show your knowledge and tackle most of what comes your way.

For me, these are basically my notes from preparing for coding interviews, and I figured I’d share them in case they help you too.

We also plan to share a quiz app featuring these same questions in a simple, game-like environment. We’re building it now and will share it with you soon, so stay tuned!

Enough said! Let’s start with some simple questions and gradually go deeper. In each post, I’ll also include links to the next parts of the series.

Each answer in this series isn’t meant to be 100% complete or the final word. The goal is to show the interviewer that you understand the topic well and can explain it clearly. There’s always room for improvement or deeper discussion.

Some questions include additional context to strengthen your understanding or explore the topic more deeply — we’ve marked those with a 🧠 icon.

Others will point you to related resources for continued learning or best practices — those are marked with a 🚀 icon.

Introductory coding interview questions

What is Express.js?


Express.js is a web application framework built on Node.js. It provides a higher-level API for handling HTTP requests. Express.js is commonly used to build REST APIs because it simplifies route handling compared to Node.js.

What are some alternatives to Express.js?

  • Koa.js – Created by the same team behind Express
  • Fastify – Focuses on speed and performance
  • Nest.js – TypeScript-first framework

Why should you use Express.js over Node’s built-in HTTP module?

Node.js provides a low-level foundation, while Express abstracts common patterns, such as routing and middleware, making development faster and more maintainable.

🧠 Deeper – Compare Express and Node Doing the Same Thing

It’s easy sometimes to say something is different, but it would be nice to have a quick glimpse at an actual example. Because of this, below is a simple GET / users route implemented first with Express.js and then with plain Node.js so you can see the practical difference in code size and readability.

// Express.js

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

app.get('/users', (req, res) => {
  res.json({ message: 'Users list' });
});

app.listen(3000, () => console.log('Express server on port 3000'));
JavaScript

In Express.js if you use res.json() (or pass an object/array to res.send()), Express automatically sets the Content-Type header to application/json; charset=utf-8. You don’t need to call res.setHeader() yourself.

// Node
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/users') {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({ message: 'Users list' }));
  } else {
    res.statusCode = 404;
    res.end('Not Found');
  }
});

server.listen(3000, () => console.log('Node server on port 3000'));
JavaScript

Middlewares

What is middleware in Express?

A middleware is a function. It runs before the final route handler, and it has access to the request, response, and next callback.

An example of a middleware on a route level could be:

// auth middleware
function auth(req, res, next) {
  if (!checkAuthOfUserId(req.params.id)) {
    return res.status(403).send('Access denied');
  }
  next();
}

app.get('/users/:id', auth, (req, res) => {
  res.send(`Access granted to user ${req.params.id}`);
});
JavaScript

How many types of middleware do you know?

There are types of middleware:

  • Global
  • Path based
  • Route specific
  • Error handling

Examples for each would be:

Global type middleware example

const express = require('express');

const app = express();

app.use(express.json());
JavaScript

This middleware will run for all incoming requests, regardless of route or method.

Path-specific middleware example

// middleware definition
function adminLogger(req, res, next) { ... }

// Apply middleware to all /admin paths (any method)
app.use('/admin', adminLogger);

app.get('/admin', (req, res) => {
  res.send('Admin Home');
});

app.post('/admin', (req, res) => {
  res.send('Admin POST');
});

app.get('/admin/settings', (req, res) => {
  res.send('Admin Settings');
});
JavaScript

A path-specific middleware will run on all HTTP methods for any route that starts with that path.

Route-based middleware example

function requireAuth(req, res, next) { ... }

app.get('/profile', requireAuth, (req, res) => {
  res.send('Welcome to your profile!');
});
JavaScript

This middleware is applied only to a specific route and will run only when a GET request is made to the /profile endpoint.

Error-handling middleware example

Error-handling middleware is a special feature in Express.js that catches errors and handles them in a single location. It has four parameters:

function errorHandler(err, req, res, next) {
  // Custom error handling logic
}
JavaScript

In Express 5, if a route handler or middleware returns a Promise that is either rejected or throws an exception, Express will automatically call next(err) for you.

What do you know about the app.use function?

app.use() is a method in Express used to register middleware functions. These middleware run before any route handler and can modify the request or response, or end the response cycle.

🚀 Further reading – Using the Right Terms — Path, Route, or Handler?

When working with Express.js, you’ll frequently encounter the terms route, path, and handler. It’s important to understand the distinction between them, as each plays a different role in handling HTTP requests.

Let’s break it down using the following example:

app.get('/users', (req, res) => { ... });
JavaScript

Path: The URL pattern that the server listens to — here, it’s '/users'.

Route: The combination of an HTTP method (e.g., GET, POST) and a path. In this example, GET /users is the route.

Handler: The callback function that is executed when a request matches the route. In this case, it’s the function (req, res) => { ... }.

Routes

What is a route in Express.js?

A route in Express defines a specific endpoint by combining an HTTP method (such as GET, POST, etc.) with a path, along with the logic (called a handler) that should execute when that request is received.

Example

app.get('/hello', (req, res) => {
  res.send('Hello this is route!')
})
JavaScript

What’s the difference between app.get and app.post?

Both app.get and app.post define routes.

  • app.get() handles GET requests for fetching data.
  • app.post() handles POST requests for updating or creating data.

How would you retrieve a route param from a URL like /users/:id?

app.get('/users/:id', (req, res) => {
  const { id } = req.params;
  res.send(`User id is ${id}`)
})
JavaScript

What’s the difference between req.params, req.query, and req.body?

Property
Source
Example
req.params
Route parameters
/users/:idreq.params.id
req.query
URL query string
/search?q=testreq.query.q
req.body
POST/PUT body
req.body.name

needs middleware like express.json()

Can a route have multiple middleware functions? How?

Yes, you could add middleware by adding it to the route before the handler.

app.get('/secure', authMiddleware, loggingMiddleware, (req, res) => {
  res.send('Access granted');
});
JavaScript

What is express.Router() and why would you use it?

express.Router() creates a modular, mini Express app. It helps organize code by separating routes into files.

For example, you might organize all your user-related routes in a separate file, such as:

// routes/users.js

const express = require('express');
const router = express.Router();

// GET /users
router.get('/', (req, res) => {
  res.send('User list');
});

// GET /users/:id
router.get('/:id', (req, res) => {
  res.send(`User ID: ${req.params.id}`);
});

module.exports = router;
JavaScript

and then import and register them in your main Express application file like this:

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

const userRoutes = require('./routes/users');

// Mount user router at /users
app.use('/users', userRoutes);

app.listen(3000, () => console.log('Server running'));
JavaScript

What’s next?

In this post, we reviewed key Express.js concepts — from routing and middleware types to error handling. These are the kinds of topics that come up often in coding interviews.

In the next part of this series, we’ll shift gears toward project-level questions. Stay tuned!

Leave a reply

Your email address will not be published. Required fields are marked *