Challenge - 5 Problems
Middleware vs Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding Middleware Purpose
In Node.js web frameworks, what is the primary role of middleware functions?
Attempts:
2 left
💡 Hint
Think about how requests are processed step-by-step in a server.
✗ Incorrect
Middleware functions in Node.js frameworks like Express act as layers that process requests and responses in order. They can modify data, end the request, or pass control to the next middleware.
❓ component_behavior
intermediate2:00remaining
Decorator Pattern Behavior
What does a decorator function typically do in Node.js when applied to another function?
Attempts:
2 left
💡 Hint
Think about how you might add extra features to a function without rewriting it.
✗ Incorrect
A decorator wraps a function to add new behavior before or after the original function runs, keeping the original intact.
📝 Syntax
advanced2:00remaining
Middleware Function Syntax
Which of the following is the correct syntax for a middleware function in Express.js?
Attempts:
2 left
💡 Hint
Middleware needs to call next() to continue the chain.
✗ Incorrect
Middleware functions take three parameters: request, response, and next. Calling next() passes control to the next middleware.
🔧 Debug
advanced2:00remaining
Decorator Pattern Bug Identification
Given this decorator function, which option will cause a runtime error when used to decorate a function?
Node.js
function decorator(fn) {
return function(...args) {
console.log('Before');
const result = fn(...args);
console.log('After');
return result;
};
}Attempts:
2 left
💡 Hint
Consider what happens if fn is not a function.
✗ Incorrect
Passing null instead of a function causes a TypeError when trying to call fn(...args).
❓ state_output
expert3:00remaining
Middleware vs Decorator Output Difference
Consider this Express middleware and decorator usage. What will be logged when a request hits the server?
Node.js
const express = require('express'); const app = express(); function logMiddleware(req, res, next) { console.log('Middleware start'); next(); console.log('Middleware end'); } function logDecorator(fn) { return function(req, res) { console.log('Decorator start'); fn(req, res); console.log('Decorator end'); }; } app.use(logMiddleware); app.get('/', logDecorator((req, res) => { console.log('Handler'); res.send('Hello'); }));
Attempts:
2 left
💡 Hint
Think about the order middleware and route handlers run in Express.
✗ Incorrect
Middleware runs first, then the decorated handler logs 'Decorator start', then 'Handler', then 'Decorator end', then middleware logs 'Middleware end' after next() completes.