Resource-based URL design helps organize web addresses so they clearly show what data or action they relate to. It makes websites easier to understand and use.
Resource-based URL design in Express
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Express
app.METHOD('/resource/:id', handlerFunction)METHOD is the HTTP method like GET, POST, PUT, DELETE.
Use :id to capture a specific resource identifier from the URL.
Examples
Express
app.get('/users', (req, res) => { /* list users */ })
Express
app.get('/users/:id', (req, res) => { /* get user by id */ })
Express
app.post('/users', (req, res) => { /* create new user */ })
Express
app.put('/users/:id', (req, res) => { /* update user by id */ })
Sample Program
This Express app uses resource-based URLs for books. You can get all books or a single book by its ID using clear, simple URLs.
Express
import express from 'express'; const app = express(); app.use(express.json()); // List all books app.get('/books', (req, res) => { res.json([{ id: 1, title: 'Book One' }, { id: 2, title: 'Book Two' }]); }); // Get a book by ID app.get('/books/:id', (req, res) => { const bookId = Number(req.params.id); const books = [{ id: 1, title: 'Book One' }, { id: 2, title: 'Book Two' }]; const book = books.find(b => b.id === bookId); if (book) { res.json(book); } else { res.status(404).send('Book not found'); } }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
Use plural nouns for resource names (e.g., /books, /users) to keep URLs consistent.
Keep URLs simple and predictable to help users and developers understand them easily.
Use HTTP methods (GET, POST, PUT, DELETE) to represent actions on resources clearly.
Summary
Resource-based URLs organize web addresses around data items like users or books.
They use clear paths and HTTP methods to show what action happens.
This design makes APIs easier to use and maintain.
Practice
1. What is the main idea behind resource-based URL design in Express?
easy
Solution
Step 1: Understand resource-based URL design
This design organizes URLs by resources such as users or books, making them clear and meaningful.Step 2: Compare options
Options B, C, and D do not follow this clear organization principle.Final Answer:
Organize URLs around data items like users or books -> Option DQuick Check:
Resource-based URLs = Organize by data items [OK]
Hint: Think: URLs should name data items clearly [OK]
Common Mistakes:
- Thinking URLs should be random or unclear
- Ignoring HTTP methods in design
- Putting all actions under one URL
2. Which Express route correctly follows resource-based URL design to get a user by ID?
easy
Solution
Step 1: Identify correct HTTP method and URL pattern
To get a user by ID, use GET method and URL with resource and ID: '/user/:id'.Step 2: Check options
app.get('/user/:id', handler) uses GET and '/user/:id' which is correct. Others use wrong methods or URLs.Final Answer:
app.get('/user/:id', handler) -> Option AQuick Check:
GET + /resource/:id = correct get route [OK]
Hint: GET method + resource path with :id for fetching [OK]
Common Mistakes:
- Using POST instead of GET for fetching
- Using generic paths like '/getUser'
- Missing :id parameter in URL
3. What will be the response if the following Express route is called with DELETE /books/123?
app.delete('/books/:bookId', (req, res) => {
res.send(`Deleted book ${req.params.bookId}`);
});medium
Solution
Step 1: Understand route and HTTP method
The route listens for DELETE requests on '/books/:bookId'. The parameter bookId will be '123' from the URL.Step 2: Check response behavior
The handler sends a string with the bookId inserted, so response is 'Deleted book 123'.Final Answer:
"Deleted book 123" -> Option CQuick Check:
DELETE /books/:id returns message with id [OK]
Hint: Route param replaces :bookId in response [OK]
Common Mistakes:
- Confusing parameter name with literal string
- Expecting 404 if route exists
- Thinking syntax error occurs
4. Identify the error in this Express route for updating a user:
app.put('/users', (req, res) => {
const id = req.params.id;
res.send(`Updated user ${id}`);
});medium
Solution
Step 1: Check URL path and parameter usage
The route URL is '/users' but code tries to read req.params.id which requires ':id' in path.Step 2: Identify mismatch
Since ':id' is missing in URL, req.params.id will be undefined causing error or wrong behavior.Final Answer:
Missing :id parameter in URL path -> Option BQuick Check:
URL must include :id to access req.params.id [OK]
Hint: Check if URL path matches params used in code [OK]
Common Mistakes:
- Ignoring missing :id in URL
- Confusing req.params with req.body
- Thinking PUT is wrong method here
5. You want to design Express routes for a blog API with posts and comments. Which URL design best follows resource-based principles for updating a comment with ID 45 on post with ID 10?
hard
Solution
Step 1: Understand nested resource URLs
Comments belong to posts, so URL should reflect this hierarchy: '/posts/:postId/comments/:commentId'.Step 2: Check options for correct pattern
app.put('/posts/10/comments/45', handler) uses full nested path with IDs, matching resource-based design. Others miss nesting or IDs.Final Answer:
app.put('/posts/10/comments/45', handler) -> Option AQuick Check:
Nested resource URLs = /posts/:postId/comments/:commentId [OK]
Hint: Nest related resources in URL with IDs [OK]
Common Mistakes:
- Using generic action names in URL
- Omitting parent resource ID
- Ignoring resource hierarchy
