0
0
Expressframework~20 mins

API versioning strategies in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding URL Path Versioning
In Express.js, what is the main advantage of using URL path versioning like /api/v1/resource for API versioning?
AIt automatically updates the API version without changing client requests.
BIt hides the API version from clients to simplify URLs.
CIt allows clients to specify the API version in the URL, making it easy to route requests to different handlers.
DIt forces all clients to use the latest API version only.
Attempts:
2 left
💡 Hint
Think about how the server knows which version of the API to use based on the request URL.
component_behavior
intermediate
2:00remaining
Header Versioning Behavior
Given an Express.js API that uses a custom header X-API-Version to determine the API version, what happens if a client sends a request without this header?
Express
app.use((req, res, next) => {
  const version = req.headers['x-api-version'] || '1';
  if(version === '1') {
    // handle v1
    res.send('Response from API v1');
  } else if(version === '2') {
    // handle v2
    res.send('Response from API v2');
  } else {
    res.status(400).send('Unsupported API version');
  }
});
AThe server responds with 'Response from API v2' by default.
BThe server responds with 'Response from API v1' because it defaults to version 1.
CThe server crashes due to missing header.
DThe server responds with 'Unsupported API version' error.
Attempts:
2 left
💡 Hint
Look at how the version variable is assigned when the header is missing.
📝 Syntax
advanced
2:00remaining
Middleware Version Routing Syntax
Which Express.js middleware setup correctly routes requests to different API versions using URL path versioning?
Express
const express = require('express');
const app = express();

// Version 1 handler
const v1Router = express.Router();
v1Router.get('/users', (req, res) => res.send('Users from v1'));

// Version 2 handler
const v2Router = express.Router();
v2Router.get('/users', (req, res) => res.send('Users from v2'));

// Mount routers here
// ???

app.listen(3000);
A
app.use('/api/v1', v1Router);
app.use('/api/v2', v2Router);
B
app.use('/v1/api', v1Router);
app.use('/v2/api', v2Router);
C
app.use('/api', v1Router);
app.use('/api', v2Router);
D
app.use(v1Router);
app.use(v2Router);
Attempts:
2 left
💡 Hint
Think about how the URL path should include the version segment before the resource path.
🔧 Debug
advanced
2:00remaining
Debugging Query Parameter Versioning
Consider this Express.js code snippet that tries to version an API using a query parameter ?version=2. What is the bug causing all requests to respond with 'Version 2 response' regardless of the query parameter?
Express
app.get('/api/users', (req, res) => {
  const version = req.query.version;
  if(version === '2') {
    res.send('Version 2 response');
  } else {
    res.send('Version 1 response');
  }
});
AThe route path should include the version like /api/v2/users.
BThe query parameter should be accessed as req.params.version, not req.query.version.
CThe res.send calls are missing return statements.
DThe code uses assignment (=) instead of comparison (===) in the if condition.
Attempts:
2 left
💡 Hint
Check the if condition syntax carefully.
state_output
expert
2:00remaining
Output of Combined Versioning Middleware
What is the output when a client sends a GET request to /api/users with header X-API-Version: 2 given this Express.js code?
Express
const express = require('express');
const app = express();

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

app.get('/api/users', (req, res) => {
  switch(req.apiVersion) {
    case '1':
      res.send('Users from API v1');
      break;
    case '2':
      res.send('Users from API v2');
      break;
    default:
      res.status(400).send('Unsupported API version');
  }
});

app.listen(3000);
AUsers from API v2
BUsers from API v1
CUnsupported API version
DError: Cannot read property 'apiVersion' of undefined
Attempts:
2 left
💡 Hint
Look at how the middleware sets req.apiVersion and how the route uses it.