Interview Q&A / Node.js
Node.js interview questions
The event loop on the server, streams, modules and building APIs with Express.
Prefer rapid revision? Practise Node.js short interview answers →
- 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 - 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 - 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 - 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 - 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