0
0
Expressframework~10 mins

API versioning strategies in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - API versioning strategies
Client sends request
Check version info
Match version strategy
URL versioning
Header versioning
Query param versioning
Accept header versioning
Route to correct API handler
Send response
The server checks the client's version info using different strategies, then routes the request to the matching API version handler.
Execution Sample
Express
const express = require('express');
const app = express();

app.get('/v1/users', (req, res) => res.send('Users v1'));
app.get('/v2/users', (req, res) => res.send('Users v2'));

app.listen(3000);
This code shows URL versioning by using different URL paths for API versions.
Execution Table
StepRequest URLVersion DetectedRouting ActionResponse Sent
1/v1/usersv1Route to /v1/users handlerUsers v1
2/v2/usersv2Route to /v2/users handlerUsers v2
3/users?version=1No version in URL pathNo match, 404 or fallbackError or fallback response
4/usersNo version infoNo match, 404 or fallbackError or fallback response
💡 Requests without version info in URL path do not match any route and stop with error or fallback.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Request URL/v1/users/v2/users/users?version=1/users
Version DetectedNonev1v2NoneNone
Routing ActionNoneRoute to /v1/users handlerRoute to /v2/users handlerNo matchNo match
Response SentNoneUsers v1Users v2Error or fallbackError or fallback
Key Moments - 2 Insights
Why does the request to '/users?version=1' not match any route in the example?
Because the example only defines versioning in the URL path (like '/v1/users'), not in query parameters. So '/users?version=1' does not match any defined route (see execution_table row 3).
How does the server know which version to use when using URL versioning?
It looks at the URL path prefix like '/v1/' or '/v2/' to detect the version and route accordingly (see execution_table rows 1 and 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is sent when the request URL is '/v2/users'?
AUsers v1
BUsers v2
CError or fallback response
DNo response
💡 Hint
Check execution_table row 2 under 'Response Sent'
At which step does the version detection fail to find a version in the URL path?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at 'Version Detected' column in execution_table rows 3 and 4
If we add a header versioning strategy, how would the routing action change for '/users' with header 'API-Version: 1'?
ARoute to /v1/users handler
BNo match, 404 or fallback
CRoute to /v2/users handler
DRoute to default handler
💡 Hint
Header versioning uses headers to detect version, so routing can match even if URL path lacks version
Concept Snapshot
API versioning lets servers handle multiple API versions.
Common strategies:
- URL versioning: /v1/resource
- Header versioning: custom header like 'API-Version'
- Query param versioning: ?version=1
Server checks version info, routes to matching handler.
Helps keep old clients working while updating API.
Full Transcript
API versioning strategies help servers manage different versions of an API. The server checks the client's request for version information using methods like URL path prefixes, headers, or query parameters. Based on the detected version, the server routes the request to the correct version handler. For example, URL versioning uses paths like '/v1/users' or '/v2/users' to distinguish versions. If a request lacks version info or uses an unsupported method, the server may return an error or fallback response. This approach allows APIs to evolve without breaking existing clients.