0
0
Expressframework~5 mins

app.all and app.use for catch-all in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does app.all do in Express?

app.all matches all HTTP methods (GET, POST, PUT, DELETE, etc.) for a specific route path.

It is useful when you want the same handler for all request types on that path.

Click to reveal answer
beginner
How does app.use differ from app.all in Express?

app.use is used to mount middleware functions that run for all HTTP methods and paths that start with the given path.

It is often used for middleware like logging, authentication, or catch-all routes.

Click to reveal answer
intermediate
How can app.all be used as a catch-all route?

By using app.all('*', handler), you can catch all requests to any path and any HTTP method that were not matched earlier.

This is useful for 404 pages or default responses.

Click to reveal answer
intermediate
What is the common use of app.use as a catch-all in Express?

Using app.use((req, res) => { ... }) without a path catches all requests not handled by previous routes.

This is often used to send a 404 response or handle errors.

Click to reveal answer
beginner
Which method should you place last in your Express app to handle unmatched routes?

You should place either app.all('*', handler) or app.use((req, res) => { ... }) at the end of your route definitions.

This ensures all unmatched requests are caught and handled properly.

Click to reveal answer
What does app.all('/path', handler) do in Express?
AHandles all HTTP methods for '/path'
BHandles only GET requests for '/path'
CHandles all paths for GET requests
DMounts middleware for all paths
Which Express method is best to use for middleware that runs on every request regardless of path?
Aapp.post()
Bapp.all()
Capp.get()
Dapp.use()
How do you create a catch-all route for unmatched requests using app.all?
Aapp.all('/', handler)
Bapp.all('*', handler)
Capp.all('', handler)
Dapp.all('/catch', handler)
What happens if you place app.use middleware before route handlers?
AIt only runs after routes
BIt will never run
CIt runs before the routes and can modify requests
DIt causes an error
Which is a good use case for app.use((req, res) => { res.status(404).send('Not found') })?
AHandling unmatched routes with 404
BLogging requests
CServing static files
DParsing JSON bodies
Explain how app.all and app.use can be used to catch all unmatched routes in an Express app.
Think about how Express matches routes and middleware in order.
You got /5 concepts.
    Describe the difference between app.all and app.use in Express and when to use each for catch-all behavior.
    Focus on routes vs middleware roles.
    You got /5 concepts.