Interview Q&A / Node.js

INTERVIEW Q&A · 68 QUESTIONS

Node.js interview questions

The event loop on the server, streams, modules and building APIs with Express.

  1. What is middleware in Express.js and how does next() work?

    Middleware is a function that runs between the request arriving and the response being sent. It receives req, res and next, and it either ends the response or calls next() to pass control to the next middleware or route. Calling next(err) jumps to error-handling middleware.

    BeginnerIn-depth2 min read
  2. How do you return a custom 404 response for unknown routes in Express?

    Add a middleware with no path after all your routes. If no earlier route replied, the request reaches it, and you send a 404 status with a JSON or HTML body.

    BeginnerQuick answer1 min read
  3. How do you serve static files such as images and CSS in Express?

    Use the built-in express.static middleware with a folder path. Files inside that folder are served directly by URL, and you can add a prefix and caching options.

    BeginnerQuick answer1 min read
  4. What is express.Router and why use it?

    express.Router creates a mini app that holds a group of related routes and middleware. You mount it under a path prefix, which keeps large apps organised by feature.

    BeginnerQuick answer1 min read
  5. What is the difference between route parameters and query strings in Express?

    Route parameters are part of the path, like /users/42, and identify a specific resource, read from req.params. Query strings follow a question mark, like ?page=2, and filter or tweak a request, read from req.query.

    BeginnerQuick answer1 min read
  6. Why does req.body come out undefined in Express, and what does express.json do?

    Express does not read request bodies by itself. express.json() is middleware that parses JSON bodies with a JSON content type and places the result on req.body. Without it, req.body is undefined.

    BeginnerQuick answer1 min read
  7. Why does the order of middleware and routes matter in Express?

    Express runs middleware and routes in the order they are registered, and a response ends the chain. A parser placed after a route never runs for it, and a catch-all placed early swallows later routes.

    BeginnerQuick answer1 min read
  8. Write an Express middleware that checks a JWT and protects a route.

    Read the Bearer token from the Authorization header, verify it with jwt.verify, attach the claims to req.user and call next. On a missing or invalid token, respond 401 without calling next.

    BeginnerQuick answerCoding1 min read