0
0
Node.jsframework~20 mins

API versioning strategies in Node.js - 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
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.