0
0
Expressframework~10 mins

Router level middleware in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Router-level middleware
Request comes in
Router receives request
Router-level middleware runs
Middleware calls next()
Router handles route or passes to next middleware
Response sent or next middleware runs
When a request hits a router, router-level middleware runs first. It can modify the request or response, then calls next() to continue to route handlers or other middleware.
Execution Sample
Express
const express = require('express');
const router = express.Router();

router.use((req, res, next) => {
  console.log('Middleware runs');
  next();
});

router.get('/', (req, res) => res.send('Hello'));

module.exports = router;
This code sets up router-level middleware that logs a message before handling a GET request.
Execution Table
StepActionMiddleware Callednext() CalledRoute Handler CalledOutput
1Request to '/' received by routerNoNoNoNo output yet
2Router-level middleware runsYesYesNoLogs 'Middleware runs'
3Route handler for '/' runsNoNoYesSends 'Hello' response
4Response sent to clientNoNoNoClient receives 'Hello'
💡 Response sent after route handler completes, middleware chain ends
Variable Tracker
VariableStartAfter MiddlewareAfter Route HandlerFinal
req.url////
res.headersSentfalsefalsetruetrue
middlewareCalledfalsetruetruetrue
responseSentfalsefalsetruetrue
Key Moments - 2 Insights
Why does the route handler run only after middleware calls next()?
Because in the execution_table row 2, middleware calls next(), which tells Express to continue to the next function, the route handler.
What happens if middleware does not call next()?
The request stops at middleware and the route handler never runs, so no response is sent (see execution_table step 2 importance of next()).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the middleware log 'Middleware runs'?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check the 'Middleware Called' and 'Output' columns in execution_table row 2
According to variable_tracker, when does res.headersSent become true?
AAfter middleware runs
BAfter route handler runs
CAt the start
DNever
💡 Hint
Look at 'res.headersSent' values in variable_tracker after route handler
If middleware did not call next(), what would happen to the route handler?
AIt would run anyway
BIt would run before middleware
CIt would not run
DIt would run twice
💡 Hint
Refer to key_moments explanation about next() importance
Concept Snapshot
Router-level middleware runs when a request hits a router.
It can modify req or res, then must call next() to continue.
If next() is not called, request stops and route handlers don't run.
Use router.use() to add middleware to a router.
Middleware runs before route handlers inside that router.
Full Transcript
Router-level middleware in Express runs when a request is received by a router. The middleware function can do tasks like logging or modifying the request. It must call next() to pass control to the next middleware or route handler. If next() is not called, the request stops and no response is sent. After middleware calls next(), the router's route handler runs and sends the response. Variables like req.url stay the same, while res.headersSent changes to true after the response is sent. This flow ensures middleware can prepare or check requests before routes handle them.