Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express module.
Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' to import the framework.
✗ Incorrect
The Express module is imported using require('express').
2fill in blank
mediumComplete the code to add a middleware that assigns a unique request ID.
Express
app.use((req, res, next) => {
req.id = [1]();
next();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
Date.now or Math.random which are not guaranteed unique.✗ Incorrect
Using uuid.v4() generates a unique request ID for tracing.
3fill in blank
hardFix the error in the code to correctly import the UUID v4 function.
Express
const { [1] } = require('uuid'); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'uuidv4' or 'uuidV4' which are not exported names.
✗ Incorrect
The correct import syntax is const { v4 } = require('uuid');.
4fill in blank
hardFill both blanks to log the request ID and method in the middleware.
Express
app.use((req, res, next) => {
console.log('Request ID:', req.[1], 'Method:', req.[2]);
next();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
req.url or req.body instead of req.method.✗ Incorrect
Request ID is stored in req.id and HTTP method in req.method.
5fill in blank
hardFill all three blanks to create a middleware that adds a request ID header and logs it.
Express
app.use((req, res, next) => {
const id = [1]();
req.[2] = id;
res.setHeader('[3]', id);
next();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong header name or not assigning the ID to
req.id.✗ Incorrect
The middleware generates a UUID with v4(), assigns it to req.id, and sets the X-Request-ID header.