0
0
Expressframework~20 mins

Application-level middleware in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Middleware Master
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 the following Express app code. What will the client receive when making a GET request to '/'?
Express
const express = require('express');
const app = express();

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

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

app.get('/', (req, res) => {
  res.send(req.customValue);
});
A"Hello"
B"Hello World"
C"World"
DError: req.customValue is undefined
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object before the route handler.
lifecycle
intermediate
2:00remaining
In what order are these middlewares executed?
Given this Express app setup, what is the order of console logs when a GET request to '/' is made?
Express
const express = require('express');
const app = express();

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

app.get('/', (req, res, next) => {
  console.log('Route handler');
  next();
});

app.use((req, res) => {
  console.log('Last middleware');
  res.send('Done');
});
AFirst middleware -> Route handler -> Last middleware
BFirst middleware -> Last middleware -> Route handler
CRoute handler -> First middleware -> Last middleware
DLast middleware -> First middleware -> Route handler
Attempts:
2 left
💡 Hint
Remember that app.use middlewares run in the order they are added.
🔧 Debug
advanced
2:00remaining
Why does this cause a 'headers already sent' error?
Examine this Express app. Why does the server throw an error when making a GET request to '/stop'?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  if (req.path === '/stop') {
    res.send('Stopped here');
    return;
  }
  next();
});

app.get('/stop', (req, res) => {
  res.send('This will never run');
});
ABecause next() is called after res.send(), causing headers to be sent twice
BBecause res.send() is missing inside the if block
CBecause the middleware does not call next() when path is '/stop'
DBecause the route handler is missing for '/stop'
Attempts:
2 left
💡 Hint
Think about what happens when next() is called after sending a response.
📝 Syntax
advanced
2:00remaining
Which middleware definition is syntactically correct?
Identify the correct syntax for an Express application-level middleware function.
Aapp.use((req, res, next) => { next; })
Bapp.use((req, res) => { next(); })
Capp.use((req, res, next) => { res.send('Hi') next(); })
Dapp.use((req, res, next) => { res.send('Hi'); })
Attempts:
2 left
💡 Hint
Middleware functions must have three parameters and call next() properly if not ending response.
state_output
expert
3:00remaining
What is the final value of req.processed after all middleware run?
Given this Express app, what is the value of req.processed when the client requests '/'?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  req.processed = 1;
  next();
});

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

app.get('/', (req, res) => {
  req.processed *= 3;
  res.send(req.processed.toString());
});
A"3"
B"1"
C"9"
D"6"
Attempts:
2 left
💡 Hint
Track the changes to req.processed step by step through middleware and route.