0
0
Node.jsframework~30 mins

Middleware vs decorator pattern in Node.js - Hands-On Comparison

Choose your learning style9 modes available
Understanding Middleware vs Decorator Pattern in Node.js
📖 Scenario: You are building a simple Node.js server that processes requests. You want to understand how middleware and decorator patterns can help add features like logging and timing without changing the main logic.
🎯 Goal: Build a small Node.js example showing how to use middleware functions and a decorator function to add logging and timing features to a simple request handler.
📋 What You'll Learn
Create a basic request handler function named handleRequest that returns a string.
Create a middleware function named loggerMiddleware that logs the request info.
Create a middleware function named timerMiddleware that measures execution time.
Create a decorator function named decorateHandler that wraps handleRequest to add logging and timing.
Show how to apply middleware functions in sequence.
Show how to apply the decorator function to the handler.
💡 Why This Matters
🌍 Real World
Middleware and decorator patterns are common in Node.js servers to add features like logging, authentication, and timing without changing core logic.
💼 Career
Understanding these patterns helps developers write clean, modular, and maintainable server code, a key skill for backend development roles.
Progress0 / 4 steps
1
Create the basic request handler function
Create a function called handleRequest that takes no arguments and returns the string 'Request handled'.
Node.js
Need a hint?

Define a simple function that returns a fixed string.

2
Create middleware functions for logging and timing
Create two middleware functions: loggerMiddleware and timerMiddleware. Each takes a next function as argument and returns a new function. loggerMiddleware should log 'Logging request' before calling next(). timerMiddleware should record start time, call next(), then log the elapsed time in milliseconds with console.log.
Node.js
Need a hint?

Each middleware returns a function that calls next() with added behavior before or after.

3
Apply middleware functions in sequence to the handler
Create a variable called composedHandler that applies loggerMiddleware and timerMiddleware to handleRequest in this order: first loggerMiddleware, then timerMiddleware. Use function calls to wrap handleRequest accordingly.
Node.js
Need a hint?

Wrap handleRequest first with loggerMiddleware, then wrap that result with timerMiddleware.

4
Create a decorator function to add logging and timing
Create a function called decorateHandler that takes a function fn as argument and returns a new function. The returned function should log 'Decorator logging', record start time, call fn(), then log the elapsed time with console.log. Then create a variable decoratedHandler by calling decorateHandler with handleRequest.
Node.js
Need a hint?

The decorator returns a new function that adds logging and timing around the original function call.