Express.js interview questions

16 questions from the Express.js Fundamentals: Routing, Middleware and APIs course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting Started

1. What does `app.listen(3000)` do?

  1. Registers a new route
  2. Parses incoming JSON
  3. Starts the server and binds it to port 3000
  4. Creates a new route
Show answer

Answer: Starts the server and binds it to port 3000

`listen` binds and starts the underlying HTTP server on the given port.

Lesson: Your First Server

Module 2: Routing

2. For `GET /users/5?active=true`, where does `5` end up?

  1. req.query.id
  2. req.body.id
  3. req.headers.id
  4. req.params.id (if the route is /users/:id)
Show answer

Answer: req.params.id (if the route is /users/:id)

Path segments captured with `:name` go to `req.params`; only text after `?` goes to `req.query`.

Lesson: Query Strings

Module 3: Middleware

3. What happens if a middleware never calls next() or sends a response?

  1. The request hangs — the client waits forever
  2. Express skips to the next route automatically
  3. Node throws a compile error
  4. It restarts the server
Show answer

Answer: The request hangs — the client waits forever

Without next() or a response, the request-response cycle never completes and the client times out.

Lesson: Writing Custom Middleware

Module 5: Handling JSON Bodies

4. Which status code fits a request rejected for bad input?

  1. 500
  2. 400
  3. 200
  4. 201
Show answer

Answer: 400

400 Bad Request signals the client sent an invalid payload; 500 implies a server-side failure instead.

Lesson: Basic Input Validation

Module 7: Error Handling

5. How many arguments must an error-handling middleware function accept?

  1. 2
  2. 3
  3. 4
  4. 1
Show answer

Answer: 4

Express identifies error handlers purely by arity — exactly (err, req, res, next).

Lesson: Error-Handling Middleware

Module 9: Connecting to a Database

6. What is Mongoose?

  1. A CSS framework
  2. A replacement for Express itself
  3. A template engine
  4. An ODM for modeling MongoDB data in Node
Show answer

Answer: An ODM for modeling MongoDB data in Node

Mongoose is an Object Data Modeling library that sits on top of the MongoDB driver.

Lesson: Express + a Database

Module 11: CORS

7. CORS restrictions are enforced by...

  1. The browser, based on response headers
  2. The server, before sending a response
  3. The database
  4. The reverse proxy only
Show answer

Answer: The browser, based on response headers

The server always processes the request; it's the browser that blocks the JS caller from reading the response if headers don't allow it.

Lesson: Enabling CORS

8. What does the helmet package do?

  1. Sets protective HTTP security headers
  2. Parses JSON bodies
  3. Connects to MongoDB
  4. Compresses images
Show answer

Answer: Sets protective HTTP security headers

Helmet applies a set of secure default headers.

Lesson: Security: Helmet & Rate Limiting

Module 13: Testing & Deployment

9. What is Express middleware?

  1. A database
  2. A template
  3. A function (req, res, next) that runs during a request
  4. A CSS file
Show answer

Answer: A function (req, res, next) that runs during a request

Middleware inspects requests and calls next.

Lesson: Deploying an Express App

10. Where do route parameters like :id appear?

  1. req.query
  2. req.body
  3. req.headers
  4. req.params
Show answer

Answer: req.params

Colon segments are captured into req.params.

Lesson: Deploying an Express App

11. Which middleware parses JSON bodies?

  1. express.json()
  2. express.static()
  3. express.Router()
  4. cors()
Show answer

Answer: express.json()

Without it req.body is undefined.

Lesson: Deploying an Express App

12. How many arguments does error middleware take?

  1. 2
  2. 4
  3. 3
  4. 1
Show answer

Answer: 4

(err, req, res, next) identifies an error handler.

Lesson: Deploying an Express App

13. Which status code suits invalid input?

  1. 200
  2. 500
  3. 400
  4. 301
Show answer

Answer: 400

400 Bad Request signals a client error.

Lesson: Deploying an Express App

14. What does express.Router help with?

  1. Parsing forms
  2. Hashing passwords
  3. Serving fonts
  4. Organizing routes into modular files
Show answer

Answer: Organizing routes into modular files

Routers mount groups of routes under a prefix.

Lesson: Deploying an Express App

15. Who enforces CORS restrictions?

  1. The browser
  2. The database
  3. Express
  4. The OS
Show answer

Answer: The browser

Servers send headers; browsers decide.

Lesson: Deploying an Express App

16. Why export app without calling listen for tests?

  1. It is faster to write
  2. supertest can use the app without opening a port
  3. listen is deprecated
  4. It enables HTTPS
Show answer

Answer: supertest can use the app without opening a port

Tests import the app and make in-process requests.

Lesson: Deploying an Express App

← Back to the course