Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set up a basic Express server.
Node.js
const express = require('[1]'); const app = express(); app.listen(3000, () => console.log('Server running'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' to create the server.
✗ Incorrect
The Express library is required to create the server.
2fill in blank
mediumComplete the code to define a versioned API route using URL path versioning.
Node.js
app.use('/api/[1]/users', (req, res) => res.send('User list'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api' instead of a version number in the path.
✗ Incorrect
Using 'v1' in the URL path is a common way to version APIs.
3fill in blank
hardFix the error in the code to read the API version from a custom header.
Node.js
app.use((req, res, next) => {
const version = req.headers['[1]'];
if (version === '1') next();
else res.status(400).send('Invalid API version');
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard header names that don't exist.
✗ Incorrect
Custom headers often use the 'x-' prefix, like 'x-api-version'.
4fill in blank
hardFill both blanks to implement query parameter versioning in Express.
Node.js
app.get('/users', (req, res) => { const version = req.query.[1]; if (version === '[2]') res.send('User list v1'); else res.status(400).send('Unsupported version'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'v' as the query parameter name instead of 'version'.
✗ Incorrect
The query parameter 'version' is checked for the value 'v1' to serve version 1 of the API.
5fill in blank
hardFill all three blanks to implement header versioning with middleware.
Node.js
app.use((req, res, next) => {
const version = req.headers['[1]'];
if (version === '[2]') {
req.apiVersion = '[3]';
next();
} else {
res.status(400).send('Invalid API version');
}
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api-version' without the 'x-' prefix.
✗ Incorrect
The middleware reads the 'x-api-version' header, checks for 'v1', and sets the internal version to '1'.