Express router.all() Method
What you’ll learn
- How
router.all(path, ...handlers)works in router modules. - How to combine shared checks with method-specific routes.
- How to use
next()correctly in route flow. - How to avoid duplicate sends and hanging requests.
Syntax
javascript
router.all(path, handler)
router.all(path, middleware1, middleware2, handler)1
Apply auth guard for all methods
javascript
var router = require('express').Router();
router.all('/admin/*', function (req, res, next) {
if (!req.user) return res.status(401).send('Unauthorized');
next();
});2
Continue to method-specific handlers
javascript
router.get('/admin/users', function (req, res) {
res.json({ users: ['Asha', 'Kavin'] });
});
router.post('/admin/users', function (req, res) {
res.status(201).json({ created: req.body });
});3
Log all methods on one router path
javascript
router.all('/orders/:id', function (req, res, next) {
console.log(req.method, req.originalUrl);
next();
});⚠️ Common pitfalls
- Not calling
next()when downstream handlers should run. - Sending a response in
router.all()and then attempting another send later. - Using over-broad wildcard paths that match more routes than expected.
❓ FAQ
It registers a route handler on a router that matches all HTTP methods for the given path.
Behavior is the same, but router.all() is used inside router modules created by express.Router().
Use it for shared route-level checks like auth, input guards, tracing, or context setup.
Usually yes. router.all() handles shared logic, then method-specific handlers return final responses.
If you do not send a response and do not call next(), the request can hang.
Did you know?
router.all() applies one route-level handler to every HTTP method, making it useful for auth checks, logging, and shared setup.
4 people found this page helpful
