Suggest an editImprove this articleRefine the answer for “Middleware order”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Express runs middleware in the order they were registered in the code - top to bottom: global `app.use()` calls first, then path-bound ones, then Routers, then specific route handlers, and finally error handlers (the 4-argument kind). **Key point:** if a middleware doesn't call `next()` and doesn't end the response, the chain stops there and the request "hangs" - the following handlers never run.Shown above the full answer for quick recall.Answer (EN)Image## 1. The basic idea > Express runs middleware **in the order they were registered in the code** - top to bottom. That is, the order in your file is the order requests get processed in. Every middleware can: - handle part of the request; - modify `req` or `res`; - either end the response (`res.send()` / `res.end()`), - or pass execution onward by calling `next()`. ### Example: ```javascript const express = require('express'); const app = express(); app.use((req, res, next) => { console.log('First middleware'); next(); }); app.use((req, res, next) => { console.log('Second middleware'); next(); }); app.get('/', (req, res) => { console.log('Route handler'); res.send('Done!'); }); app.listen(3000); ``` Output for `GET /`: ```javascript First middleware Second middleware Route handler ``` ## 2. The rule: "everything top to bottom" Express registers middleware **in the order they're written**: - `app.use()`, global middleware; - `app.get()`, `app.post()`, routes; - `app.use('/path', router)`, nested routes; - `app.use((err, req, res, next) => {...})`, an error handler (at the end!). If some middleware **doesn't call** `next()` and doesn't end the response (`res.send()`), the chain **stops**. ### Example: a missing `next()` ```javascript app.use((req, res, next) => { console.log('1'); // forgot next()! }); app.get('/', (req, res) => { console.log('2'); res.send('OK'); }); ``` The result: Express gets stuck at the first middleware, because `next()` was never called, the second handler **never runs**. ## 3. Middleware bound to paths You can specify that a middleware applies only to a certain path: ```javascript app.use('/admin', (req, res, next) => { console.log('Middleware for /admin'); next(); }); app.get('/admin', (req, res) => res.send('Admin panel')); app.get('/user', (req, res) => res.send('Profile')); ``` For `GET /admin`, the middleware runs → then the route. For `GET /user`, the `/admin` middleware **is skipped**. ## 4. Router and local middleware When you mount a router (`express.Router()`), all the middleware inside it runs **within a local context**, but the order between global and local middleware is preserved. ```javascript const router = express.Router(); router.use((req, res, next) => { console.log('Router middleware'); next(); }); router.get('/', (req, res) => res.send('The /api route')); app.use((req, res, next) => { console.log('Global middleware'); next(); }); app.use('/api', router); ``` For a request to `/api`: ```javascript Global middleware Router middleware ``` ## 5. Error-handling middleware runs last > Error handlers must come **after all regular middleware and routes**. > Express identifies them by their **four arguments**: > `(err, req, res, next)` ```javascript app.use((err, req, res, next) => { console.error('Error:', err.message); res.status(500).send('Something went wrong'); }); ``` Express calls them **only if** you pass an error via `next(err)` or an exception is thrown inside a middleware. ## 6. A summary of the execution order | Stage | What happens | Example | |---|---|---| | 1 | Global middleware (`app.use`) | logging, JSON parsing | | 2 | Path-bound middleware (`app.use('/path', ...)`) | authorization, caching | | 3 | Middleware and routes inside `express.Router()` | API endpoints | | 4 | Specific route handlers (`app.get`, `app.post`) | the main code | | 5 | Error-handling middleware (`app.use(err, req, res, next)`) | final error handling | ## 7. Summary > Express determines middleware execution order **by the order they're registered in the code**. > > The rules: > > 1. Execution runs **top to bottom**. > 2. Every middleware must call `next()` to pass control onward. > 3. Middleware can be **scoped to a path** (`app.use('/admin', ...)`). > 4. **Error handlers** (with 4 arguments) run last. > 5. If a middleware doesn't call `next()` and doesn't send a response, the request "hangs".For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.