0
0
Node.jsframework~10 mins

API versioning strategies in Node.js - Interactive Code Practice

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

Complete 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'
Apath
Bhttp
Cexpress
Dfs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' to create the server.
2fill in blank
medium

Complete 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'
Av1
Bv2
Cversion1
Dapi
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api' instead of a version number in the path.
3fill in blank
hard

Fix 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'
Aapi-version
Bapi_version
Cversion
Dx-api-version
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard header names that don't exist.
4fill in blank
hard

Fill 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'
Aversion
Bv
Cv1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'v' as the query parameter name instead of 'version'.
5fill in blank
hard

Fill 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'
Ax-api-version
Bv1
C1
Dapi-version
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api-version' without the 'x-' prefix.