0
0
Expressframework~20 mins

Middleware execution flow (req, res, next) in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express middleware sequence?
Consider this Express app code with three middleware functions. What will be sent to the client when a GET request is made to '/'?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  req.message = 'Hello';
  next();
});

app.use((req, res, next) => {
  req.message += ' World';
  next();
});

app.get('/', (req, res) => {
  res.send(req.message);
});
AError: req.message is undefined
B"Hello"
C"World"
D"Hello World"
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object and calls next() to pass control.
lifecycle
intermediate
2:00remaining
Which middleware runs last before the response is sent?
Given this Express middleware setup, which middleware function executes immediately before the response is sent to the client?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use((req, res, next) => {
  console.log('Middleware 2');
  next();
});

app.get('/', (req, res) => {
  console.log('Route handler');
  res.send('Done');
});
ARoute handler
BMiddleware 2
CNo middleware runs before response
DMiddleware 1
Attempts:
2 left
💡 Hint
Remember the order middleware and route handlers run in Express.
🔧 Debug
advanced
2:00remaining
Why does this middleware never call the next middleware?
Examine this middleware. Why does the next middleware not run after this one?
Express
app.use((req, res, next) => {
  if (!req.user) {
    res.status(401).send('Unauthorized');
  } else {
    next();
  }
});

app.use((req, res, next) => {
  console.log('Second middleware');
  next();
});
ABecause res.send() ends the response and next() is not called when !req.user
BBecause next() is called twice causing an error
CBecause the middleware is not registered properly
DBecause req.user is always truthy
Attempts:
2 left
💡 Hint
Think about what happens when res.send() is called without next().
📝 Syntax
advanced
2:00remaining
What error does this middleware code produce?
Identify the error caused by this middleware function code.
Express
app.use((req, res, next) => {
  console.log('Start');
  next();
  console.log('End');
});
ANo error, middleware runs fine
BReferenceError: next is not defined
CSyntaxError: Unexpected identifier
DTypeError: next is not a function
Attempts:
2 left
💡 Hint
Look carefully at how next is used.
state_output
expert
3:00remaining
What is the final value of req.count after this middleware chain?
Given this middleware chain, what is the value of req.count when the response is sent?
Express
app.use((req, res, next) => {
  req.count = 1;
  next();
});

app.use((req, res, next) => {
  req.count += 2;
  next();
});

app.use((req, res, next) => {
  req.count *= 3;
  next();
});

app.get('/', (req, res) => {
  res.send(req.count.toString());
});
A"7"
B"9"
C"3"
D"6"
Attempts:
2 left
💡 Hint
Calculate step-by-step how req.count changes in each middleware.