0
0
Node.jsframework~20 mins

Middleware vs decorator pattern in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware vs Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Middleware Purpose
In Node.js web frameworks, what is the primary role of middleware functions?
ATo handle HTTP requests and responses in a sequence, allowing modification or termination of the request flow.
BTo manage database connections and queries automatically.
CTo compile JavaScript code into machine code for faster execution.
DTo decorate classes by adding new methods or properties at runtime.
Attempts:
2 left
💡 Hint
Think about how requests are processed step-by-step in a server.
component_behavior
intermediate
2:00remaining
Decorator Pattern Behavior
What does a decorator function typically do in Node.js when applied to another function?
ADeletes the original function from memory to save space.
BAutomatically logs all errors thrown by the function without changing it.
CReplaces the original function with a completely unrelated function.
DWraps the original function to extend or modify its behavior without changing its code.
Attempts:
2 left
💡 Hint
Think about how you might add extra features to a function without rewriting it.
📝 Syntax
advanced
2:00remaining
Middleware Function Syntax
Which of the following is the correct syntax for a middleware function in Express.js?
Afunction(req, res, next) { console.log('Middleware'); next(); }
Bfunction(req, res) { console.log('Middleware'); next(); }
Cfunction(err, req, res, next) { console.log('Middleware'); next(); }
Dfunction(req, res, next) { console.log('Middleware'); }
Attempts:
2 left
💡 Hint
Middleware needs to call next() to continue the chain.
🔧 Debug
advanced
2: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;
  };
}
Aconst decorated = decorator(function() { return 42; }); decorated();
Bconst decorated = decorator(null); decorated();
Cconst decorated = decorator(() => 'ok'); decorated();
Dconst decorated = decorator(() => { throw new Error('Fail'); }); decorated();
Attempts:
2 left
💡 Hint
Consider what happens if fn is not a function.
state_output
expert
3: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');
}));
A
Decorator start
Middleware start
Handler
Decorator end
Middleware end
B
Middleware start
Handler
Decorator start
Decorator end
Middleware end
C
Middleware start
Decorator start
Handler
Decorator end
Middleware end
D
Middleware start
Decorator start
Decorator end
Handler
Middleware end
Attempts:
2 left
💡 Hint
Think about the order middleware and route handlers run in Express.