---
id: 6a06cf65e8fcb644d2a04a29
title: Express Middleware Quiz
challengeType: 8
dashedName: quiz-express-middleware
---

# --description--

To pass the quiz, you must correctly answer at least 18 of the 20 questions below.

# --quizzes--

## --quiz--

### --question--

#### --text--

What is middleware in Express?

#### --distractors--

A function that starts the Express server and binds it to a port.

---

A configuration object that defines routes and their handlers.

---

A built-in database connector for handling persistent data.

#### --answer--

A function that executes during the lifecycle of a request to the server.

### --question--

#### --text--

What three things does a middleware function have access to?

#### --distractors--

The database connection, the session store, and the route definition.

---

The server config, the port number, and the app instance.

---

The environment variables, the request headers, and the response status.

#### --answer--

The request object (`req`), the response object (`res`), and the `next` function.

### --question--

#### --text--

What happens if a middleware function does not call `next()`?

#### --distractors--

Control is passed automatically to the next middleware in the stack.

---

Express throws an unhandled error and crashes the server.

---

The request is retried from the beginning of the middleware stack.

#### --answer--

The request-response cycle stops.

### --question--

#### --text--

What does `app.use()` do?

#### --distractors--

Defines a `GET` route handler for a specific path.

---

Starts the server and begins listening for incoming connections.

---

Creates a new instance of the Express application.

#### --answer--

Adds middleware globally to the application.

### --question--

#### --text--

In what order is middleware executed in an Express app?

#### --distractors--

In reverse order, starting from the last middleware added.

---

Alphabetically, based on the middleware function names.

---

Randomly, depending on which request arrives first.

#### --answer--

In the order it is added to the app.

### --question--

#### --text--

What is application-level middleware bound to?

#### --distractors--

A specific route instance created with `express.Router()`.

---

The Node.js HTTP server returned by `app.listen()`.

---

An individual route handler defined with `app.get()` or `app.post()`.

#### --answer--

An instance of the Express app.

### --question--

#### --text--

Which of the following can be used to register application-level middleware?

#### --distractors--

`app.listen()` or route-specific methods like `app.get()` and `app.delete()`

---

`app.route()` or route-specific methods like `app.patch()` and `app.options()`

---

`router.use()` or route-specific methods like `router.get()` and `router.post()`

#### --answer--

`app.use()` or route-specific methods like `app.get()` and `app.post()`

### --question--

#### --text--

What is router-level middleware bound to?

#### --distractors--

The main Express app instance created with `express()`.

---

The Node.js `http` module's server object.

---

A specific route handler callback function.

#### --answer--

An instance of `express.Router()`.

### --question--

#### --text--

Which method is used to apply router-level middleware?

#### --distractors--

`app.use()`

---

`express.use()`

---

`app.router()`

#### --answer--

`router.use()`

### --question--

#### --text--

How do you scope router-level middleware to a specific path in your app?

#### --distractors--

By calling `router.scope('/path')` before defining routes.

---

By passing the path directly to `router.use()` in the main file.

---

By setting `router.basePath = '/path'` before mounting it.

#### --answer--

By mounting the router with `app.use('/path', router)`.

### --question--

#### --text--

What makes error-handling middleware different from regular middleware in Express?

#### --distractors--

It uses `res.error()` instead of `res.send()` to respond to the client, bypassing the normal response flow.

---

It must be defined before all other routes so it can intercept errors before they reach any route handler.

---

It is registered with `app.error()` instead of `app.use()`, which signals to Express it should handle errors.

#### --answer--

It has four parameters (`err`, `req`, `res`, and `next`), and the `err` parameter tells Express it is an error handler.

### --question--

#### --text--

When is error-handling middleware invoked?

#### --distractors--

When a route handler returns a response with a 4xx or 5xx status code.

---

When the server receives more than one request at the same time.

---

When the request takes longer than the default timeout to complete.

#### --answer--

When an error is passed to `next(err)`.

### --question--

#### --text--

Where must error-handling middleware be placed in the middleware stack?

#### --distractors--

At the top, before any routes are defined.

---

Immediately after the route that is most likely to throw an error.

---

At the same level as other middleware, in any position.

#### --answer--

At the end, after all other middleware and routes.

### --question--

#### --text--

What does the built-in `express.json()` middleware do?

#### --distractors--

Serves JSON files stored in the public directory to the client.

---

Converts the response object to JSON before sending it to the client.

---

Validates that all incoming requests contain a valid JSON content-type header.

#### --answer--

Parses incoming requests with JSON payloads.

### --question--

#### --text--

What does `express.static()` do?

#### --distractors--

Parses incoming requests with URL-encoded form data.

---

Caches route handler responses to improve performance.

---

Registers static route paths that cannot be changed at runtime.

#### --answer--

Serves static files such as images, CSS, and JavaScript.

### --question--

#### --text--

What does `express.urlencoded()` parse, and where is the result available?

#### --distractors--

It parses JSON payloads and makes the result available on `req.json`.

---

It parses query strings from the URL and makes them available on `req.query`.

---

It parses multipart form data and makes the files available on `req.files`.

#### --answer--

It parses URL-encoded payloads from HTML forms and makes the data available on `req.body`.

### --question--

#### --text--

What does the third-party `cors` middleware do?

#### --distractors--

Logs all incoming HTTP requests with method, URL, and response time.

---

Parses cookies from incoming requests and attaches them to `req.cookies`.

---

Compresses response bodies to reduce the size of data sent to the client.

#### --answer--

Enables Cross-Origin Resource Sharing.

### --question--

#### --text--

Which of the following correctly classifies Express middleware?

#### --distractors--

`morgan` is built-in while `express.static()` is third-party

---

`cors` is built-in while `morgan` is third-party

---

`express.urlencoded()` is built-in while `express.json()` is third-party

#### --answer--

`express.json()` is built-in while `cors` is third-party

### --question--

#### --text--

What does the third-party middleware `morgan` do?

#### --distractors--

Enables Cross-Origin Resource Sharing.

---

Parses cookies from incoming requests and attaches them to `req.cookies`.

---

Compresses response bodies to reduce the size of data sent to the client.

#### --answer--

Logs HTTP requests.

### --question--

#### --text--

What happens when a middleware function calls `next()`?

#### --distractors--

The request is immediately sent back to the client with a 200 status.

---

The current middleware function restarts from the beginning.

---

Express skips all remaining middleware and executes the final route handler.

#### --answer--

Control passes to the next middleware or route handler in the stack.
