Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express middleware test?
Consider this Express middleware that adds a custom header. What will the test output when the middleware is called?
Express
const express = require('express'); const request = require('supertest'); const app = express(); function customHeader(req, res, next) { res.set('X-Custom-Header', 'TestValue'); next(); } app.use(customHeader); app.get('/', (req, res) => res.send('Hello')); request(app) .get('/') .end((err, res) => { if (err) throw err; console.log(res.headers['x-custom-header']); });
Attempts:
2 left
💡 Hint
Think about what the middleware does before calling next() and how headers are set.
✗ Incorrect
The middleware sets the header 'X-Custom-Header' to 'TestValue' before calling next(), so the response includes this header.
❓ state_output
intermediate2:00remaining
What is the value of req.processed after this middleware chain?
Given these two middlewares, what will be the value of req.processed in the final handler?
Express
const express = require('express'); const app = express(); function firstMiddleware(req, res, next) { req.processed = 1; next(); } function secondMiddleware(req, res, next) { req.processed += 2; next(); } app.use(firstMiddleware); app.use(secondMiddleware); app.get('/', (req, res) => { res.send(String(req.processed)); });
Attempts:
2 left
💡 Hint
Remember that middlewares run in order and can modify the request object.
✗ Incorrect
The first middleware sets req.processed to 1, the second adds 2, so the final value is 3.
🔧 Debug
advanced2:00remaining
Why does this middleware test fail with a timeout?
This test for an Express middleware hangs and eventually times out. What is the cause?
Express
function faultyMiddleware(req, res, next) {
// Missing next call
}
const express = require('express');
const request = require('supertest');
const app = express();
app.use(faultyMiddleware);
request(app)
.get('/')
.expect(200)
.end((err, res) => {
if (err) throw err;
});Attempts:
2 left
💡 Hint
Think about what happens if next() is not called.
✗ Incorrect
The middleware never calls next(), so Express waits for next middleware or response end, causing a hang.
📝 Syntax
advanced2:00remaining
Which option correctly tests async middleware with error handling?
Given an async middleware that may throw an error, which test code correctly handles the error?
Express
async function asyncMiddleware(req, res, next) { try { await someAsyncOperation(); next(); } catch (err) { next(err); } }
Attempts:
2 left
💡 Hint
Consider how to handle errors in async middleware tests with supertest.
✗ Incorrect
Option A correctly expects a 500 error and calls done properly based on error presence.
🧠 Conceptual
expert3:00remaining
Which testing strategy best isolates middleware behavior?
When testing Express middleware, which approach best isolates the middleware's behavior from the rest of the app?
Attempts:
2 left
💡 Hint
Think about how to test middleware without interference from other parts.
✗ Incorrect
Calling middleware directly with mocks isolates its logic without involving Express internals or other middleware.