0
0
Expressframework~20 mins

Middleware factory pattern in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this middleware factory produce?
Consider this Express middleware factory function. What will be the output when a request hits the route using this middleware?
Express
function greetMiddleware(greeting) {
  return function(req, res, next) {
    res.locals.message = `${greeting}, user!`;
    next();
  };
}

app.use(greetMiddleware('Hello'));
app.get('/', (req, res) => {
  res.send(res.locals.message);
});
AThe response will be 'Hello, user!'
BThe response will be 'greeting, user!'
CThe server will crash with an error
DThe response will be undefined
Attempts:
2 left
💡 Hint
Think about how the factory function uses the greeting parameter inside the returned middleware.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this middleware factory
Which option correctly fixes the syntax error in this middleware factory code?
Express
function authMiddleware(role) {
  return (req, res, next) => {
    if(req.user.role !== role) {
      res.status(403).send('Forbidden');
    } else {
      next()
    }
  }
}
ANo syntax error, code is correct
BAdd curly braces around the else block
CAdd a missing closing parenthesis after next()
DAdd a semicolon after next() call
Attempts:
2 left
💡 Hint
Check if arrow functions require braces or semicolons in this context.
🔧 Debug
advanced
2:00remaining
Why does this middleware factory not work as expected?
This middleware factory is supposed to log a custom message. Why does it always log 'undefined'?
Express
function logMessage(msg) {
  return function(req, res, next) {
    console.log(message);
    next();
  };
}

app.use(logMessage('Request received'));
AThe middleware does not call next(), so it hangs
BThe variable 'message' is not defined; should use 'msg' instead
CThe factory function does not return a function
DThe console.log syntax is incorrect
Attempts:
2 left
💡 Hint
Check variable names inside the returned middleware function.
state_output
advanced
2:00remaining
What is the value of res.locals after this middleware runs?
Given this middleware factory and usage, what will be the value of res.locals.user after the middleware runs?
Express
function userMiddleware(name) {
  return (req, res, next) => {
    res.locals.user = { name };
    next();
  };
}

app.use(userMiddleware('Alice'));

app.get('/', (req, res) => {
  res.json(res.locals.user);
});
A{"user":"Alice"}
B{}
C{"name":"Alice"}
Dundefined
Attempts:
2 left
💡 Hint
Look at how res.locals.user is assigned inside the middleware.
🧠 Conceptual
expert
2:00remaining
Why use a middleware factory instead of a simple middleware?
Which reason best explains why middleware factories are useful in Express?
AThey replace the need for route handlers entirely
BThey improve server performance by caching middleware
CThey automatically handle errors without extra code
DThey allow creating middleware with custom parameters for reuse
Attempts:
2 left
💡 Hint
Think about how you can customize middleware behavior.