📖 Scenario: You are building a simple Node.js API for a bookstore. Over time, you want to add new features without breaking existing clients. To do this, you will implement API versioning strategies.
🎯 Goal: Create a basic Node.js API with two versions (v1 and v2) using URL path versioning. You will set up initial data, configure versioning, implement version-specific routes, and complete the server setup.
📋 What You'll Learn
Create an initial list of books with exact titles and authors
Add a version prefix variable for API routes
Implement two GET routes for versions v1 and v2
Complete the Express server setup to listen on port 3000
💡 Why This Matters
🌍 Real World
APIs often evolve over time. Versioning helps keep old clients working while adding new features.
💼 Career
Understanding API versioning is essential for backend developers working on scalable and maintainable services.
Progress0 / 4 steps
1
DATA SETUP: Create initial book data
Create a constant array called books with these exact objects: { id: 1, title: 'Node Basics', author: 'Alice' } and { id: 2, title: 'Advanced Node', author: 'Bob' }.
Node.js
Hint
Use const books = [ ... ] with two objects inside.
2
CONFIGURATION: Add API version prefix
Create a constant called apiVersion and set it to the string 'v1'.
Node.js
Hint
Use const apiVersion = 'v1'; to set the version prefix.
3
CORE LOGIC: Implement versioned GET routes
Import Express, create an app with express(), and add two GET routes: /api/v1/books that returns the books array, and /api/v2/books that returns the books array with an added year property (2024) for each book.
Node.js
Hint
Use app.get with the exact paths /api/v1/books and /api/v2/books. For v2, add year: 2024 to each book.
4
COMPLETION: Start the server on port 3000
Add app.listen(3000) with a callback that logs 'Server running on port 3000'.
Node.js
Hint
Use app.listen(3000, () => console.log('Server running on port 3000')).
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
Step 1: Understand common API versioning methods
API versioning can be done via URL path, headers, query params, or content negotiation.
Step 2: Identify the simplest and most common method
Including the version in the URL path is easiest to implement and understand.
Final Answer:
Including the version number in the URL path, like /v1/users -> Option D
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'); });
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
Step 1: Examine the if condition syntax
The condition uses = which assigns '1' to req.query.version instead of comparing.
Step 2: Understand the effect of assignment in condition
Assignment returns '1' (truthy), so the if block always runs, ignoring else.
Final Answer:
Using assignment (=) instead of comparison (== or ===) in the if condition -> Option B
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
Step 1: Separate versioning methods logically
Use URL path for v1 routes explicitly, e.g., /api/v1/....
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.
Final Answer:
Define separate routes: /api/v1/... for v1, and /api/... with header check for v2 -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.