Bird
Raised Fist0
Node.jsframework~20 mins

API versioning strategies in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
API Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding URL Path Versioning

Which URL correctly represents version 2 of an API using URL path versioning?

A/api/v2/users
B/api/users?v=2
C/v2.api/users
D/api/users/version2
Attempts:
2 left
💡 Hint

URL path versioning places the version number directly in the URL path.

❓ component_behavior
intermediate
2:00remaining
Behavior of Header Versioning in Express.js

Given an Express.js API that uses a custom header X-API-Version to determine the API version, what happens if the header is missing?

Node.js
app.use((req, res, next) => {
  const version = req.headers['x-api-version'] || '1';
  req.apiVersion = version;
  next();
});

app.get('/data', (req, res) => {
  if (req.apiVersion === '2') {
    res.send('Data from version 2');
  } else {
    res.send('Data from version 1');
  }
});
AThe API returns a 404 Not Found error.
BThe API throws an error due to missing header.
CThe API responds with 'Data from version 2' regardless.
DThe API responds with 'Data from version 1' as default.
Attempts:
2 left
💡 Hint

Look at how the version is assigned when the header is missing.

📝 Syntax
advanced
2:00remaining
Correct Syntax for Query Parameter Versioning in Express.js

Which Express.js route handler correctly reads the API version from a query parameter version and sends a response accordingly?

Node.js
app.get('/info', (req, res) => {
  // Your code here
});
Aconst version = req.query.version || '1'; if(version === '2') res.send('Version 2 info'); else res.send('Version 1 info');
Bconst version = req.params.version || '1'; if(version === '2') res.send('Version 2 info'); else res.send('Version 1 info');
Cconst version = req.headers['version'] || '1'; if(version === '2') res.send('Version 2 info'); else res.send('Version 1 info');
Dconst version = req.body.version || '1'; if(version === '2') res.send('Version 2 info'); else res.send('Version 1 info');
Attempts:
2 left
💡 Hint

Query parameters are accessed via req.query in Express.js.

🔧 Debug
advanced
2:00remaining
Debugging Version Conflict in Accept Header Versioning

Consider this Express.js middleware that reads API version from the Accept header:

app.use((req, res, next) => {
  const accept = req.headers.accept;
  const versionMatch = accept.match(/application\/vnd\.myapi\.v(\d+)\+json/);
  req.apiVersion = versionMatch ? versionMatch[1] : '1';
  next();
});

What error will occur if the Accept header is missing?

ASyntaxError due to regex
BTypeError: Cannot read properties of undefined (reading 'match')
CReferenceError: accept is not defined
DNo error, defaults to version 1
Attempts:
2 left
💡 Hint

Consider what happens when accept is undefined and you call match on it.

❓ state_output
expert
2:30remaining
Output of Middleware Version Override in Express.js

Given this Express.js middleware chain, what will be the final response when a request is made to /resource with header X-API-Version: 3?

app.use((req, res, next) => {
  req.apiVersion = '1';
  next();
});

app.use((req, res, next) => {
  if(req.headers['x-api-version']) {
    req.apiVersion = req.headers['x-api-version'];
  }
  next();
});

app.get('/resource', (req, res) => {
  res.send(`API version is ${req.apiVersion}`);
});
AAPI version is 1
BAPI version is undefined
CAPI version is 3
DAPI version is x-api-version
Attempts:
2 left
💡 Hint

Think about the order of middleware and how they modify req.apiVersion.

Practice

(1/5)
1. Which of the following is the simplest and most common method to version a Node.js API?
easy
A. Using content negotiation with the Accept header
B. Using a custom HTTP header to specify the version
C. Adding a version query parameter, like ?version=1
D. Including the version number in the URL path, like /v1/users

Solution

  1. Step 1: Understand common API versioning methods

    API versioning can be done via URL path, headers, query params, or content negotiation.
  2. Step 2: Identify the simplest and most common method

    Including the version in the URL path is easiest to implement and understand.
  3. Final Answer:

    Including the version number in the URL path, like /v1/users -> Option D
  4. Quick Check:

    URL path versioning = simplest method [OK]
Hint: URL path versioning is easiest to spot and use [OK]
Common Mistakes:
  • Thinking headers are simpler for beginners
  • Confusing query params with URL path
  • Assuming content negotiation is the default
2. Which syntax correctly defines a route in Express.js for version 2 of an API using URL path versioning?
easy
A. app.get('/api/v2/users', (req, res) => { res.send('v2 users'); });
B. app.get('/api/users?v=2', (req, res) => { res.send('v2 users'); });
C. app.get('/api/users', (req, res) => { if(req.headers['version'] === '2') res.send('v2 users'); });
D. app.get('/api/users', (req, res) => { res.send('v2 users'); });

Solution

  1. Step 1: Recognize URL path versioning syntax in Express.js

    Version 2 in URL path means the route includes /v2/ in the path.
  2. Step 2: Match the correct route definition

    app.get('/api/v2/users', (req, res) => { res.send('v2 users'); }); uses /api/v2/users which matches version 2 in the URL path.
  3. Final Answer:

    app.get('/api/v2/users', (req, res) => { res.send('v2 users'); }); -> Option A
  4. Quick Check:

    URL path versioning route = app.get('/api/v2/users', (req, res) => { res.send('v2 users'); }); [OK]
Hint: Version in URL path means it appears in the route string [OK]
Common Mistakes:
  • Using query params instead of URL path
  • Checking headers inside route without version in path
  • Ignoring version in route path
3. Given this Express.js code snippet, what will be the response when a client requests /api/users with header Accept-Version: 2?
const express = require('express');
const app = express();

app.get('/api/users', (req, res) => {
  if (req.headers['accept-version'] === '2') {
    res.send('Users v2');
  } else {
    res.send('Users v1');
  }
});
medium
A. "Users v1"
B. 404 Not Found
C. "Users v2"
D. Error: Missing version

Solution

  1. Step 1: Check the header condition in the route handler

    The code checks if req.headers['accept-version'] equals '2'.
  2. Step 2: Determine response based on header value

    If header is '2', it sends 'Users v2'; otherwise, 'Users v1'. The request has header 'Accept-Version: 2'.
  3. Final Answer:

    "Users v2" -> Option C
  4. Quick Check:

    Header 'Accept-Version: 2' triggers v2 response [OK]
Hint: Match header value exactly to get correct version response [OK]
Common Mistakes:
  • Ignoring case sensitivity of header keys
  • Assuming URL path versioning instead of header
  • Expecting 404 when route exists
4. Identify the bug in this Express.js versioning code using query parameters:
app.get('/api/users', (req, res) => {
  if (req.query.version = '1') {
    res.send('Users v1');
  } else {
    res.send('Users v2');
  }
});
medium
A. Missing route for version 2 explicitly
B. Using assignment (=) instead of comparison (== or ===) in the if condition
C. Incorrect query parameter name; should be 'ver' not 'version'
D. res.send should be res.json for API responses

Solution

  1. Step 1: Examine the if condition syntax

    The condition uses = which assigns '1' to req.query.version instead of comparing.
  2. Step 2: Understand the effect of assignment in condition

    Assignment returns '1' (truthy), so the if block always runs, ignoring else.
  3. Final Answer:

    Using assignment (=) instead of comparison (== or ===) in the if condition -> Option B
  4. Quick Check:

    Use '==' or '===' for comparison, not '=' [OK]
Hint: Use triple equals (===) for comparisons, not single equals (=) [OK]
Common Mistakes:
  • Confusing assignment with equality check
  • Assuming missing else block causes bug
  • Thinking res.send vs res.json causes error here
5. You want to support two API versions in your Node.js app: v1 uses URL path versioning, and v2 uses a custom header X-API-Version. How should you design your Express routes to handle both cleanly?
hard
A. Define separate routes: /api/v1/... for v1, and /api/... with header check for v2
B. Use only URL path versioning for both versions to avoid confusion
C. Use query parameters for v1 and headers for v2 in the same route
D. Combine both version indicators in the URL path like /api/v1/v2/...

Solution

  1. Step 1: Separate versioning methods logically

    Use URL path for v1 routes explicitly, e.g., /api/v1/....
  2. Step 2: Use header check for v2 on routes without version in path

    Define routes like /api/... and check X-API-Version header to serve v2.
  3. Final Answer:

    Define separate routes: /api/v1/... for v1, and /api/... with header check for v2 -> Option A
  4. Quick Check:

    Separate routes for path and header versioning [OK]
Hint: Separate routes for path versioning; use header check for others [OK]
Common Mistakes:
  • Mixing both versions in one URL path
  • Using query params inconsistently
  • Ignoring header version check in routes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard