0
0
Node.jsframework~10 mins

Building custom middleware in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Building custom middleware
Request received
Middleware function called
Middleware processes request
Modify req/res objects
Perform logic (e.g., logging, auth)
Call next() to pass control
Next middleware or route handler runs
Response sent back
When a request comes in, the middleware runs, can change request or response, then calls next() to continue the flow.
Execution Sample
Node.js
function logger(req, res, next) {
  console.log(`Request: ${req.method} ${req.url}`);
  next();
}
This middleware logs the request method and URL, then passes control to the next handler.
Execution Table
StepActionreq.methodreq.urlConsole Outputnext() calledNext Handler
1Request receivedGET/homeNoNo
2Middleware logger runsGET/homeRequest: GET /homeYesNo
3next() passes controlGET/homeYesYes
4Route handler runsGET/homeYesYes
5Response sentGET/homeYesYes
💡 Middleware calls next(), so control moves to next handler and response is sent.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
req.methodGETGETGETGET
req.url/home/home/home/home
next() calledfalsetruetruetrue
Key Moments - 2 Insights
Why do we need to call next() inside middleware?
Calling next() tells Node.js to continue to the next middleware or route handler. Without it, the request will hang and never complete, as shown in execution_table step 2 where next() is called to proceed.
Can middleware modify the request or response objects?
Yes, middleware can change req or res objects before passing control. This is useful for adding data or headers. The execution_table shows req.method and req.url unchanged here, but they could be modified.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is logged to the console at step 2?
A"Request: GET /home"
B"Request: POST /login"
C"Error: no next() called"
D"Response sent"
💡 Hint
Check the 'Console Output' column at step 2 in execution_table.
At which step is next() first called according to the execution_table?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'next() called' column to see when it changes from No to Yes.
If next() was not called in the middleware, what would happen?
ARequest would continue to next handler
BRequest would hang and never complete
CResponse would be sent immediately
DMiddleware would run twice
💡 Hint
Refer to key_moments explanation about the importance of calling next().
Concept Snapshot
Middleware is a function with (req, res, next).
It runs on each request.
It can modify req or res.
Must call next() to continue.
Without next(), request hangs.
Used for logging, auth, etc.
Full Transcript
When a request arrives, Node.js calls middleware functions in order. Each middleware receives request and response objects and a next function. Middleware can log info, check authentication, or modify data. After its work, middleware must call next() to pass control to the next middleware or route handler. If next() is not called, the request will hang and no response will be sent. This flow ensures modular and reusable processing steps for requests.