Complete the code to set up a basic Express server.
const express = require('[1]'); const app = express(); app.listen(3000, () => console.log('Server running'));
The Express library is required to create the server.
Complete the code to define a versioned API route using URL path versioning.
app.use('/api/[1]/users', (req, res) => res.send('User list'));
Using 'v1' in the URL path is a common way to version APIs.
Fix the error in the code to read the API version from a custom header.
app.use((req, res, next) => {
const version = req.headers['[1]'];
if (version === '1') next();
else res.status(400).send('Invalid API version');
});Custom headers often use the 'x-' prefix, like 'x-api-version'.
Fill both blanks to implement query parameter versioning in Express.
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'); });
The query parameter 'version' is checked for the value 'v1' to serve version 1 of the API.
Fill all three blanks to implement header versioning with middleware.
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');
}
});The middleware reads the 'x-api-version' header, checks for 'v1', and sets the internal version to '1'.
