0
0
Node.jsframework~10 mins

Middleware vs decorator pattern in Node.js - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Middleware vs decorator pattern
Request Received
Middleware 1
Middleware 2
Next Middleware
Original Function
Decorator 1
Decorator 2
Enhance Behavior
Middleware chains process requests step-by-step, passing control along. Decorators wrap functions to add behavior before or after the original function.
Execution Sample
Node.js
const mw1 = (req, res, next) => { console.log('mw1'); next(); };
const mw2 = (req, res, next) => { console.log('mw2'); next(); };

function baseHandler() { console.log('handler'); }

const decoratedHandler = (fn) => () => { console.log('before'); fn(); console.log('after'); };

const finalHandler = decoratedHandler(baseHandler);

// Simulate middleware chain
mw1({}, {}, () => mw2({}, {}, () => baseHandler()));

// Call decorated function
finalHandler();
This code shows middleware functions calling next to pass control, and a decorator wrapping a function to add logs before and after.
Execution Table
StepFunction CalledActionOutputNext Step
1mw1Logs 'mw1', calls next()mw1Call mw2
2mw2Logs 'mw2', calls next()mw2Call baseHandler
3baseHandlerLogs 'handler'handlerEnd middleware chain
4finalHandlerLogs 'before', calls baseHandler, logs 'after'before handler afterEnd decorated call
💡 Middleware chain ends after baseHandler; decorated function ends after logging 'after'
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
req{}{}{}{}{}
res{}{}{}{}{}
nextfunctionfunctionfunctionundefinedundefined
finalHandlerfunctionfunctionfunctionfunctionfunction
Key Moments - 2 Insights
Why does middleware use 'next()' but decorators do not?
Middleware functions call 'next()' to pass control to the next middleware (see execution_table steps 1 and 2). Decorators wrap a function and call it directly inside, so no 'next()' is needed (step 4).
How does the decorator add behavior without changing the original function?
The decorator wraps the original function, adding logs before and after calling it (step 4). The original function remains unchanged but is called inside the wrapper.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is logged at Step 2?
A'mw2'
B'mw1'
C'handler'
D'before'
💡 Hint
Check the 'Output' column for Step 2 in the execution_table.
At which step does the middleware chain end?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Next Step' column to see when the middleware chain ends.
If the decorator did not call the original function, what would happen at Step 4?
AOnly 'after' would be logged
BOnly 'before' would be logged
C'handler' would still be logged
DNothing would be logged
💡 Hint
Refer to Step 4 where the decorator logs 'before', calls the function, then logs 'after'.
Concept Snapshot
Middleware pattern chains functions that receive a request and call next() to pass control.
Decorator pattern wraps a function to add behavior before and after it runs.
Middleware is used in request handling pipelines.
Decorators enhance or modify function behavior without changing the original.
Middleware calls next() to continue; decorators call the wrapped function directly.
Full Transcript
Middleware and decorator patterns are ways to add behavior to code. Middleware functions receive a request and a next function. They do something, then call next() to pass control to the next middleware. This creates a chain of processing steps. Decorators wrap a function to add extra behavior before and after calling the original function. Middleware is common in web servers to process requests step-by-step. Decorators are used to enhance functions without changing them. Middleware uses next() to continue the chain; decorators call the wrapped function inside their own code.