Express.js Server Interview Questions and Answers
Common Express.js interview questions with concise, real answers.
Express.js Server Interview Questions and Answers
Interviewers ask these Express questions often. Here are concise, real answers.
Q1: What is Express?
A minimal, flexible Node.js web framework for building APIs and web apps. You define routes, apply middlewares, and send responses. It is unopinionated and has a huge ecosystem.
Q2: What is a middleware?
A function (req, res, next) => {} that runs on every request. It can modify req, send a response, or call next to continue. Examples: json parsing, auth, logging, error handling.
Q3: How does routing work in Express?
You declare routes with app.get, app.post, etc. (or with router.get/post). The first matching route handles the request. Use express.Router() to group routes by feature.
Q4: How do you handle errors in Express?
Register an error-handling middleware at the end with four parameters: (err, req, res, next) => {}. In handlers, throw or call next(err). Use a custom ApiError class with status and message. Use an async wrapper to catch promise rejections.
Q5: What is the difference between app and router?
app is the Express application. router is a mini-app you can mount with app.use('/path', router). Routers group routes by feature (auth, user, feed). You can mount many routers on one app.
Q6: How do you parse JSON bodies?
Add app.use(express.json()) before your routes. Now req.body is the parsed JSON object for any request with Content-Type: application/json.
Q7: How do you handle cookies in Express?
Add cookie-parser middleware. Then req.cookies is the parsed cookies object. To set a cookie, use res.cookie(name, value, options) before sending the response.
Q8: How do you protect routes with JWT?
Create an auth middleware that verifies the JWT from req.cookies or the Authorization header. If valid, set req.user and call next. If not, send 401. Apply this middleware to routes that need auth.
Q9: How do you do graceful shutdown in Express?
Listen on SIGTERM, call server.close to stop accepting new connections and let in-flight requests finish, then process.exit(0). This prevents dropped requests when deploying or scaling down.
Q10: How do you scale an Express server?
Run multiple instances behind a load balancer (horizontal). Use cluster or PM2 to spawn one worker per CPU core. Add Redis for shared session state if you have any. Use Nginx in front for termination and HTTP/2.
The Takeaway
Common Express interview answers: middleware shape and use, routing with router, error handling with a four-param middleware, JSON and cookie parsing, JWT auth with middleware, graceful shutdown with SIGTERM, and scaling with cluster/PM2 + load balancer + Nginx. Be ready to talk through each.
A function (req, res, next) that runs on every request. It can modify req, send a response, or call next. Examples: json parsing, auth, logging, error handling.
Register an error-handling middleware at the end with four parameters: (err, req, res, next) => {}. In handlers, throw or call next(err). Use a custom ApiError class. Use an async wrapper to catch promise rejections.
app is the Express application. router is a mini-app you mount with app.use('/path', router). Routers group routes by feature (auth, user, feed). You can mount many routers on one app.
Add app.use(express.json()) before your routes. req.body is then the parsed JSON object for any request with Content-Type: application/json.
Run multiple instances behind a load balancer (horizontal). Use cluster or PM2 to spawn one worker per CPU core. Add Redis for shared session state if you have any. Use Nginx in front for termination and HTTP/2.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

