Complete the code to define a versioned route using URL path versioning in Express.
app.use('/api/[1]/users', userRouter);
Using /api/v1/ is a common pattern for URL path versioning in Express APIs.
Complete the code to extract the API version from a custom header in Express middleware.
const version = req.headers['[1]'];
The custom header x-api-version is commonly used to specify API version in headers.
Fix the error in the middleware that sets the API version from query parameters.
app.use((req, res, next) => {
req.apiVersion = req.query.[1];
next();
});The query parameter is commonly named 'version' to indicate the API version.
Fill both blanks to create middleware that routes requests based on API version in headers.
app.use((req, res, next) => {
const version = req.headers['[1]'];
if (version === '[2]') {
userRouterV1(req, res, next);
} else {
userRouterV2(req, res, next);
}
});The middleware reads the version from the x-api-version header and routes to userRouterV1 if the version is 'v1'.
Fill all three blanks to create a versioned API route using Accept header versioning.
app.get('/users', (req, res) => { const accept = req.headers['[1]']; if (accept === 'application/vnd.myapp.[2]+json') { res.send('User data for version [3]'); } else { res.send('Default user data'); } });
The Accept header is used to specify the API version as 'application/vnd.myapp.v1+json'. The version number '1' is shown in the response.