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. How do you handle errors in asynchronous Node.js code, and what is an unhandled promise rejection?

    Use try/catch around await, .catch() on promise chains, and the error-first argument in callbacks. An unhandled rejection is a promise that fails with no handler attached; since Node 15 it crashes the process by default, so every promise needs a catch somewhere.

    IntermediateIn-depth3 min read
  2. How does Express know a middleware is an error handler?

    By its arity: an error-handling middleware takes four arguments (err, req, res, next). It must be registered after the routes, and errors reach it through next(err) or a thrown error in synchronous code.

    IntermediateQuick answer1 min read
  3. How should a REST API return error responses consistently?

    Use one JSON shape for every error with the right HTTP status, a stable machine-readable code, a human message, and optional field details. Never leak stack traces or database errors to clients.

    IntermediateQuick answer1 min read
  4. What is the difference between stream.pipe and stream.pipeline?

    pipe connects streams but does not forward errors or clean up, so a failure can leave streams open. pipeline connects several streams, destroys all of them on error, and reports the result in one callback or promise.

    IntermediateQuick answer1 min read