0
0
Node.jsframework~20 mins

Middleware concept and execution flow in Node.js - 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 chain?
Consider this Express.js middleware setup. What will the server respond with when a GET request is made to '/'?
Node.js
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);
});

app.listen(3000);
A"Hello World"
B"Hello"
C"World"
DError: req.message is undefined
Attempts:
2 left
💡 Hint
Think about how each middleware modifies the request object before the final handler.
lifecycle
intermediate
2:00remaining
Which middleware runs first in this Express app?
Given this Express app, which middleware function executes first when a GET request to '/test' is received?
Node.js
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('/test', (req, res, next) => {
  console.log('Handler 1');
  next();
});

app.listen(3000);
AMiddleware 2
BHandler 1
CMiddleware 1
DNo middleware runs before handler
Attempts:
2 left
💡 Hint
Remember that app.use middlewares run before route handlers unless path or method restricts them.
📝 Syntax
advanced
2:00remaining
What error does this middleware code produce?
Examine this middleware function. What error will occur when a request is made?
Node.js
app.use((req, res, next) => {
  res.send('Hello');
  next();
});

app.use((req, res, next) => {
  res.send('World');
});
ANo error, works fine
BSyntaxError: Unexpected token
CTypeError: next is not a function
DError: Cannot set headers after they are sent to the client
Attempts:
2 left
💡 Hint
Think about what happens if you call next() after sending a response.
🔧 Debug
advanced
2:00remaining
Why does this middleware not modify the request as expected?
This middleware is supposed to add a user property to req, but it doesn't work. Why?
Node.js
app.use('/api', (req, res, next) => {
  req.user = { id: 1 };
  // missing next call
});

app.get('/api/data', (req, res) => {
  res.json(req.user);
});
ABecause req.user is overwritten later
BBecause next() is not called, the request hangs and never reaches the handler
CBecause the path '/api' does not match '/api/data'
DBecause res.json cannot send objects
Attempts:
2 left
💡 Hint
Middleware must call next() to continue the chain.
🧠 Conceptual
expert
3:00remaining
What is the correct order of middleware execution in Express?
Arrange these middleware and route handler steps in the order Express executes them for a matching request.
A2,3,1,4
B3,2,1,4
C2,1,3,4
D3,1,2,4
Attempts:
2 left
💡 Hint
Global middleware runs before route-specific middleware and handlers. Error middleware runs last if needed.