0
0
Expressframework~20 mins

Why advanced patterns matter in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Advanced Patterns 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 this Express app code. What will the client receive when requesting '/'?
Express
const express = require('express');
const app = express();

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

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

app.get('/', (req, res) => {
  res.send(`Value is ${req.customValue}`);
});

app.listen(3000);
AValue is 15
BValue is 5
CValue is 10
DError: req.customValue is undefined
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object in order.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines an Express error-handling middleware?
Express error-handling middleware must have a specific function signature. Which option below is correct?
Afunction (err, req, res, next) { res.status(500).send(err.message); }
Bfunction (req, res, next) { res.status(500).send('Error'); }
Cfunction (err, req, res) { res.status(500).send(err.message); }
Dfunction (req, res) { res.status(500).send('Error'); }
Attempts:
2 left
💡 Hint
Error middleware must have four parameters: err, req, res, next.
🔧 Debug
advanced
2:00remaining
Why does this Express route handler never send a response?
Look at this code snippet. Why does the client hang without receiving a response?
Express
app.get('/data', (req, res) => {
  fetchDataFromDB((err, data) => {
    if (err) {
      res.status(500).send('DB error');
    }
  });
});
ABecause fetchDataFromDB is not defined
BBecause the route handler is missing next() call
CBecause res.status(500) is missing a send() call
DBecause res.send is never called when there is no error
Attempts:
2 left
💡 Hint
Check what happens when there is no error in the callback.
🧠 Conceptual
advanced
2:00remaining
Why use the 'next' function in Express middleware?
What is the main purpose of calling next() inside Express middleware functions?
ATo send a response to the client immediately
BTo pass control to the next middleware or route handler
CTo terminate the server process
DTo log errors automatically
Attempts:
2 left
💡 Hint
Think about how Express moves through middleware layers.
state_output
expert
2:00remaining
What is the final value of 'count' after these Express middleware run?
Given this Express app code, what is the value of count when the client receives the response?
Express
const express = require('express');
const app = express();

let count = 0;

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

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

app.get('/', (req, res) => {
  count += 3;
  res.send(`Count is ${count}`);
});

app.listen(3000);
ACount is 1
BCount is 0
CCount is 6
DCount is 3
Attempts:
2 left
💡 Hint
Add the increments from each middleware and the route handler.