0
0
Expressframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Application-level middleware
Request Received
Middleware 1 Executes
Middleware 2 Executes
Route Handler Executes
Response Sent
End
The request passes through each middleware in order, then reaches the route handler, which sends the response.
Execution Sample
Express
const express = require('express');
const app = express();

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

app.get('/', (req, res) => res.send('Hello World!'));
This code sets up an Express app with one middleware that logs a message, then a route that sends a response.
Execution Table
StepActionMiddleware/HandlerConsole OutputNext CalledResponse Sent
1Request received----
2Middleware 1 runsMiddleware 1Middleware 1Yes-
3Route handler runsRoute Handler for '/'--Hello World!
4Response sent---Hello World!
5Request cycle ends----
💡 Response sent by route handler, request cycle ends.
Variable Tracker
VariableStartAfter Middleware 1After Route HandlerFinal
reqIncoming request objectPassed through Middleware 1Handled by Route HandlerRequest cycle complete
resResponse objectPassed through Middleware 1Response sentResponse cycle complete
nextFunction to call next middlewareCalled to continueNot called (end of chain)No further middleware
Key Moments - 2 Insights
Why does the route handler run after the middleware?
Because the middleware calls next(), which tells Express to continue to the next middleware or route handler, as shown in step 2 of the execution_table.
What happens if middleware does not call next()?
The request stops there and the route handler never runs, so no response is sent. This is why next() is important, as seen in the execution_table where next is called at step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is logged to the console at step 2?
A"Middleware 1"
B"Route Handler"
C"Hello World!"
DNothing
💡 Hint
Check the 'Console Output' column at step 2 in the execution_table.
At which step is the response sent to the client?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Response Sent' column in the execution_table.
If middleware did not call next(), what would happen to the route handler?
AIt would run immediately
BIt would run twice
CIt would never run
DIt would send two responses
💡 Hint
Refer to the key_moments section about the importance of calling next().
Concept Snapshot
Application-level middleware in Express runs in order for each request.
Each middleware receives req, res, and next.
Middleware must call next() to pass control.
Route handlers run after middleware chain.
Response is sent by route handler or middleware.
Middleware can modify req/res or end response early.
Full Transcript
Application-level middleware in Express is a function that runs for every request to the app or specific routes. When a request comes in, Express runs middleware functions in the order they were added. Each middleware receives the request and response objects, plus a next function. Calling next() passes control to the next middleware or route handler. If next() is not called, the request stops and no further processing happens. After middleware, the route handler runs and sends the response. This flow ensures middleware can modify requests or responses before the final handler sends data back to the client.