0
0
Expressframework~10 mins

API versioning strategies in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a versioned route using URL path versioning in Express.

Express
app.use('/api/[1]/users', userRouter);
Drag options to blanks, or click blank then click option'
Aversion1
Bv1
Capi1
Dver1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'version1' or 'ver1' instead of the shorter 'v1'.
Omitting the version in the URL path.
2fill in blank
medium

Complete the code to extract the API version from a custom header in Express middleware.

Express
const version = req.headers['[1]'];
Drag options to blanks, or click blank then click option'
Aapi-version
Bversion
Cx-api-version
Dapi_version
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api-version' without the 'x-' prefix.
Using 'version' which is too generic.
3fill in blank
hard

Fix the error in the middleware that sets the API version from query parameters.

Express
app.use((req, res, next) => {
  req.apiVersion = req.query.[1];
  next();
});
Drag options to blanks, or click blank then click option'
Aver
BapiVersion
Cv
Dversion
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase like 'apiVersion' which does not match typical query parameter naming.
Using too short keys like 'v' or 'ver' that are less clear.
4fill in blank
hard

Fill both blanks to create middleware that routes requests based on API version in headers.

Express
app.use((req, res, next) => {
  const version = req.headers['[1]'];
  if (version === '[2]') {
    userRouterV1(req, res, next);
  } else {
    userRouterV2(req, res, next);
  }
});
Drag options to blanks, or click blank then click option'
Ax-api-version
Bapi-version
Cv1
Dv2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api-version' header instead of 'x-api-version'.
Checking for 'v2' instead of 'v1' in the if condition.
5fill in blank
hard

Fill all three blanks to create a versioned API route using Accept header versioning.

Express
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');
  }
});
Drag options to blanks, or click blank then click option'
Aaccept
Bv1
C1
Dv2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'content-type' header instead of 'accept'.
Using 'v2' or wrong version numbers in the media type.
Mismatch between version string and displayed version number.