We use app.all and app.use to handle all types of requests or routes that don't match any specific path. This helps catch everything else and respond properly.
app.all and app.use for catch-all in Express
app.all(path, callback) app.use([path], callback)
app.all listens to all HTTP methods for the given path.
app.use adds middleware that runs for all HTTP methods and paths starting with the given path. If no path is given, it runs for all routes.
app.all('/example', (req, res) => { res.send('All methods on /example'); });
app.use((req, res, next) => {
console.log('Request received');
next();
});app.use('/admin', (req, res, next) => { console.log('Admin area accessed'); next(); });
app.all('*', (req, res) => { res.status(404).send('Page not found'); });
This Express app shows how app.use logs every request, app.all handles all methods on '/all', and a catch-all app.all('*') sends a 404 for unknown routes.
import express from 'express'; const app = express(); const port = 3000; // Middleware for all requests app.use((req, res, next) => { console.log(`Received ${req.method} request for ${req.url}`); next(); }); // Handle GET on /hello app.get('/hello', (req, res) => { res.send('Hello World'); }); // Catch all HTTP methods on /all app.all('/all', (req, res) => { res.send(`You made a ${req.method} request to /all`); }); // Catch all unmatched routes app.all('*', (req, res) => { res.status(404).send('Sorry, page not found'); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Order matters: Place app.use and app.all catch-alls after specific routes so they don't block them.
app.use can take a path prefix and runs for all methods and sub-paths.
Use app.all('*') to catch any route not matched earlier, often for 404 pages.
app.all handles all HTTP methods for a specific path.
app.use adds middleware for all methods and paths starting with a prefix.
Use catch-all routes to handle unknown URLs or add global middleware.
Practice
app.all('/path', handler) do in an Express app?Solution
Step 1: Understand
app.allusageapp.allis designed to handle every HTTP method (GET, POST, PUT, DELETE, etc.) but only for the exact route specified.Step 2: Compare with other methods
app.useadds middleware for all methods but for routes starting with a prefix, not just one exact path.Final Answer:
It handles all HTTP methods for the specific '/path' route. -> Option AQuick Check:
app.all= all methods, exact path [OK]
- Confusing app.all with app.use
- Thinking app.all handles all routes globally
- Assuming app.all only handles GET requests
Solution
Step 1: Understand catch-all middleware syntax
Usingapp.usewithout a path applies middleware to all routes and methods.Step 2: Analyze options
app.all('*', (req, res) => { res.send('Catch all'); });andapp.use('*', (req, res) => { res.send('Catch all'); });both specify a path '*'.app.all((req, res) => { res.send('Catch all'); });is invalid syntax.Final Answer:
app.use((req, res) => { res.send('Catch all'); }); -> Option BQuick Check:
app.use without path = catch-all middleware [OK]
- Using app.use('*', ...) which specifies a path
- Using app.all without a path argument
- Confusing app.all and app.use syntax
const express = require('express');
const app = express();
app.all('/test', (req, res, next) => {
res.write('all ');
next();
});
app.use('/test', (req, res) => {
res.end('use');
});
app.listen(3000);Solution
Step 1: Trace
For GET '/test',app.allhandlerapp.all('/test')runs first, writes 'all ' to response, then calls next() to continue.Step 2: Trace
Next,app.usehandlerapp.use('/test')matches and ends the response with 'use'. So the full response is 'all use'.Final Answer:
all use -> Option CQuick Check:
app.all writes + next() then app.use ends response [OK]
- Assuming app.use runs before app.all
- Thinking res.end in app.use causes error here
- Ignoring next() call in app.all
app.all('/api', (req, res) => {
res.send('API all methods');
next();
});Solution
Step 1: Understand res.send() behavior
res.send()ends the response and sends data to client.Step 2: Analyze next() call after res.send()
Callingnext()afterres.send()tries to continue middleware chain after response ended, causing an error.Final Answer:
Calling next() after res.send() causes an error. -> Option AQuick Check:
res.send ends response; next() after it is wrong [OK]
- Thinking next() is always safe after res.send()
- Believing app.all can't use paths
- Ignoring response end behavior
Solution
Step 1: Understand global middleware with app.use
Usingapp.usewithout a path applies middleware to all routes and methods globally.Step 2: Analyze options for logging middleware
app.all('*', (req, res, next) => { console.log(req.method, req.url); next(); });andapp.use('*', (req, res, next) => { console.log(req.method, req.url); next(); });specify a path '*'.app.all((req, res, next) => { console.log(req.method, req.url); next(); });is invalid syntax.Final Answer:
app.use((req, res, next) => { console.log(req.method, req.url); next(); }); -> Option DQuick Check:
app.use without path = global middleware [OK]
- Using app.use('*', ...) which specifies a path
- Using app.all without path argument
- Forgetting to call next() in middleware
Ready to prove it?
Pass the test and this pattern moves unknown
