All interview guides

Express.js Interview Questions and Answers

24 questions that come up in Express.js technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is clustering in Node.js/Express?

Advanced

Answer: Both b and c

Clustering spawns multiple worker processes to utilize all CPU cores. Each worker runs a separate Node.js instance. Improves performance and reliability.

2. What is Express.js?

Beginner

Answer: A minimal and flexible Node.js web application framework

Express.js is a fast, unopinionated, minimalist web framework for Node.js. It provides a robust set of features for building web and mobile applications.

3. What is Express Router?

Intermediate

Answer: Both b and c

Express Router is a complete middleware and routing system. It allows you to create modular, mountable route handlers in separate files.

4. How do you install Express.js?

Beginner

Answer: npm install express

Express is installed using npm (Node Package Manager) with the command `npm install express`. You can also use yarn: `yarn add express`.

5. How do you create a Router instance?

Intermediate

Answer: express.Router()

Create a router using `const router = express.Router()`. Then define routes on router and export it to be used with app.use().

6. How do you implement clustering?

Advanced

Answer: All of the above

Use built-in cluster module. Master process forks workers equal to CPU cores. Incoming connections distributed across workers using round-robin.

7. What is route middleware?

Intermediate

Answer: All of the above

Route middleware runs only for specific routes. Pass middleware as arguments before the handler: `app.get('/path', middleware1, middleware2, handler)`

8. How do you create a basic Express application?

Beginner

Answer: require('express')()

Create an Express app by requiring the module and calling it as a function: `const express = require('express'); const app = express();`

9. What is PM2?

Advanced

Answer: Both b and c

PM2 is a production process manager. Features: cluster mode, auto-restart on crash, log management, monitoring, zero-downtime deployments.

10. What is the purpose of error-handling middleware?

Intermediate

Answer: All of the above

Error-handling middleware has four parameters. Define it after all other middleware. It catches errors passed to next(err) or thrown in async code.

11. Which method starts an Express server?

Beginner

Answer: app.listen()

The `app.listen(port, callback)` method binds and listens for connections on the specified port. Example: `app.listen(3000, () => console.log('Server running'))`

12. What is graceful shutdown?

Advanced

Answer: All of the above

Graceful shutdown completes in-flight requests before stopping. Listen for SIGTERM/SIGINT signals, close server, close DB connections, then exit.

13. What is request/response streaming?

Advanced

Answer: All of the above

Streaming processes data in chunks without loading entire content in memory. Essential for large files. Use Node.js streams with pipe() method.

14. What is a route in Express?

Beginner

Answer: A URL path that maps to a handler function

A route determines how an application responds to a client request at a specific endpoint (URI) and HTTP method. Example: `app.get('/users', handler)`

15. How do you pass errors to error-handling middleware?

Intermediate

Answer: next(error)

Pass errors by calling `next(error)`. For synchronous code, you can throw, but it must be caught. For async code, always use next(error).

16. Which HTTP method is used to retrieve data?

Beginner

Answer: GET

GET method is used to retrieve data from the server. It should not modify server state and is idempotent (same result for multiple identical requests).

17. What is a template engine in Express?

Intermediate

Answer: Both b and c

Template engines generate HTML dynamically by combining templates with data. Popular choices: EJS, Pug (formerly Jade), Handlebars, Mustache.

18. What is caching in Express applications?

Advanced

Answer: All of the above

Caching stores frequently accessed data to reduce database load and improve response time. Use Redis, Memcached, or in-memory caches like node-cache.

19. How do you implement Redis caching?

Advanced

Answer: All of the above

Use Redis client library. Create cache middleware that checks Redis before executing handler. Set appropriate TTL (Time To Live) for cached data.

20. How do you define a GET route in Express?

Beginner

Answer: app.get(path, handler)

Define a GET route using `app.get(path, handler)`. Example: `app.get('/users', (req, res) => { res.send('Users list'); })`

21. How do you set the template engine in Express?

Intermediate

Answer: app.set('view engine', 'ejs')

Set template engine using `app.set('view engine', 'engineName')`. Also set views directory: `app.set('views', './views')`

22. What is database connection pooling?

Advanced

Answer: All of the above

Connection pooling maintains a pool of reusable database connections. Reduces overhead of creating/destroying connections. Configure pool size based on load.

23. What are the two main parameters of a route handler?

Beginner

Answer: Both a and b

Route handlers receive request (req) and response (res) objects. req contains request data, res is used to send the response back to the client.

24. How do you render a template?

Intermediate

Answer: res.render(view, data)

`res.render(view, data)` renders a view template with data and sends the resulting HTML to the client. View file extension can be omitted.

Ready to test yourself?

The full Express.js bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Express.js quiz