Bird
Raised Fist0
Expressframework~10 mins

API versioning strategies in Express - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which of the following is a common method to implement API versioning in Express?
easy
A. Using the URL path to specify the version, like /v1/users
B. Changing the database schema for each version
C. Using different port numbers for each API version
D. Renaming the Express app for each version

Solution

  1. Step 1: Understand common API versioning methods

    API versioning often uses the URL path, headers, or query parameters to distinguish versions.
  2. Step 2: Identify the correct method in Express

    Using the URL path like /v1/users is a standard and clear way to version APIs in Express.
  3. Final Answer:

    Using the URL path to specify the version, like /v1/users -> Option A
  4. Quick Check:

    URL path versioning = Using the URL path to specify the version, like /v1/users [OK]
Hint: API versions often appear in the URL path [OK]
Common Mistakes:
  • Thinking database changes are API versioning
  • Using different ports instead of URL or headers
  • Renaming the app does not affect API versioning
2. Which Express code snippet correctly sets up API versioning using URL path?
easy
A. app.get('v1/users', userRouter);
B. app.use('/v1/users', userRouter);
C. app.route('/users/v1').get(userRouter);
D. app.listen('/v1/users', userRouter);

Solution

  1. Step 1: Review Express routing syntax

    Express uses app.use(path, router) to mount routers on paths.
  2. Step 2: Identify correct versioning path usage

    Using app.use('/v1/users', userRouter); correctly mounts the router for version 1 users.
  3. Final Answer:

    app.use('/v1/users', userRouter); -> Option B
  4. Quick Check:

    Correct Express routing = app.use('/v1/users', userRouter); [OK]
Hint: Use app.use with path and router for versioning [OK]
Common Mistakes:
  • Missing leading slash in path
  • Using app.get instead of app.use for routers
  • Incorrect method like app.listen for routing
3. Given this Express code, what is the response when a client requests /api/users?
const express = require('express');
const app = express();

app.use('/api/v1/users', (req, res) => res.send('Version 1 users'));
app.use('/api/v2/users', (req, res) => res.send('Version 2 users'));

app.listen(3000);
medium
A. Cannot GET /api/users
B. Version 2 users
C. Version 1 users
D. Server error

Solution

  1. Step 1: Check defined routes

    Routes are defined only for /api/v1/users and /api/v2/users.
  2. Step 2: Analyze request path

    The request is for /api/users, which does not match any defined route.
  3. Final Answer:

    Cannot GET /api/users -> Option A
  4. Quick Check:

    Undefined route returns 404 = Cannot GET /api/users [OK]
Hint: Check exact route paths before guessing response [OK]
Common Mistakes:
  • Assuming /api/users matches /api/v1/users
  • Expecting default route without defining it
  • Confusing middleware with route handlers
4. Identify the error in this Express API versioning code:
const express = require('express');
const app = express();

app.use('/v1/users', userRouter);
app.use('/v2/users', userRouter);

app.listen(3000);
Assuming userRouter handles all user routes.
medium
A. Both versions use the same router instance, causing version conflicts
B. Missing const before userRouter
C. Routes should use app.get instead of app.use
D. No error; this is a valid versioning setup

Solution

  1. Step 1: Understand router reuse in Express

    Using the same router instance for different paths is valid and common in Express.
  2. Step 2: Check for syntax and method correctness

    Using app.use to mount routers on different paths is correct; no syntax errors present.
  3. Final Answer:

    No error; this is a valid versioning setup -> Option D
  4. Quick Check:

    Router reuse with different paths is valid = No error; this is a valid versioning setup [OK]
Hint: Reusing routers for versions is allowed in Express [OK]
Common Mistakes:
  • Thinking router reuse causes conflicts
  • Confusing app.use with app.get for routers
  • Expecting separate router instances per version
5. You want to support API versioning in Express using request headers instead of URL paths. Which code snippet correctly reads the version from the header X-API-Version and routes accordingly?
hard
A. app.use('/v1/users', userRouter); app.use('/v2/users', userV2Router);
B. app.use('/users', (req, res) => { const version = req.query.version; if (version === '1') userRouter(req, res); else userV2Router(req, res); });
C. app.use((req, res, next) => { const version = req.headers['x-api-version']; if (version === '1') userRouter(req, res, next); else if (version === '2') userV2Router(req, res, next); else res.status(400).send('Invalid API version'); });
D. app.get('/users', (req, res) => { const version = req.headers['api-version']; if (version === '1') res.send('User v1'); else res.send('User v2'); });

Solution

  1. Step 1: Identify header-based versioning approach

    Version is read from X-API-Version header in the request.
  2. Step 2: Check routing logic based on header

    app.use((req, res, next) => { const version = req.headers['x-api-version']; if (version === '1') userRouter(req, res, next); else if (version === '2') userV2Router(req, res, next); else res.status(400).send('Invalid API version'); }); reads the header, then calls the correct router or returns error if invalid.
  3. Final Answer:

    app.use((req, res, next) => { const version = req.headers['x-api-version']; if (version === '1') userRouter(req, res, next); else if (version === '2') userV2Router(req, res, next); else res.status(400).send('Invalid API version'); }); -> Option C
  4. Quick Check:

    Header-based routing = app.use((req, res, next) => { const version = req.headers['x-api-version']; if (version === '1') userRouter(req, res, next); else if (version === '2') userV2Router(req, res, next); else res.status(400).send('Invalid API version'); }); [OK]
Hint: Use middleware to check headers and route accordingly [OK]
Common Mistakes:
  • Using query parameters instead of headers
  • Not calling next() or router properly
  • Checking wrong header name