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 →
- 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 - 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 - 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 - 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 - 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