📖 Scenario: You are building a simple web server for a bookstore. You want to organize your routes by resource type to keep your code clean and easy to maintain.
🎯 Goal: Create an Express app that organizes routes for the books resource using resource-based route organization. You will set up the initial data, configure a router, add core route handlers, and complete the app to listen on a port.
📋 What You'll Learn
Create an Express app with a books array containing book objects
Create a router for books routes
Add GET route handlers for /books and /books/:id
Make the app use the books router under the /books path
Start the server listening on port 3000
💡 Why This Matters
🌍 Real World
Organizing routes by resource helps keep server code clean and maintainable, especially as projects grow.
💼 Career
Understanding Express routers and resource-based route organization is essential for backend web development jobs using Node.js.
Progress0 / 4 steps
1
Set up initial data and Express app
Create an Express app by requiring express and calling express(). Then create a books array with these exact objects: { id: 1, title: '1984' }, { id: 2, title: 'Brave New World' }, and { id: 3, title: 'Fahrenheit 451' }.
Express
Hint
Remember to require Express and call it to create your app. Then create the books array with the exact objects.
2
Create a router for books
Create a router by calling express.Router() and assign it to a variable called booksRouter.
Express
Hint
Use express.Router() to create a router for the books resource.
3
Add GET routes to the books router
Add a GET route handler on booksRouter for path '/' that sends the full books array as JSON. Also add a GET route handler on booksRouter for path '/:id' that finds a book by id from books and sends it as JSON. Use req.params.id and convert it to a number to find the book.
Express
Hint
Use booksRouter.get to add route handlers. Use res.json() to send JSON responses.
4
Use the router and start the server
Make the Express app use booksRouter for the path '/books' by calling app.use('/books', booksRouter). Then start the server listening on port 3000 using app.listen(3000).
Express
Hint
Use app.use to connect the router and app.listen to start the server.
Practice
(1/5)
1. What is the main benefit of organizing routes by resource in an Express app?
easy
A. It groups related routes together for better code organization.
B. It automatically generates HTML pages for each resource.
C. It prevents users from accessing the API without a password.
D. It makes the server run faster by caching routes.
When mounting this router in the main app with app.use('/api', router);, what is the correct URL to get the response 'Item 5'?
medium
A. /api/items/5
B. /items/5
C. /api/items
D. /items
Solution
Step 1: Understand router mounting path
The router is mounted at /api, so all routes inside it are prefixed with /api.
Step 2: Match the route for getting item with id 5
The route /items/:id inside the router becomes /api/items/:id in the app.
Final Answer:
/api/items/5 -> Option A
Quick Check:
Router prefix + route path = full URL [OK]
Hint: Add router prefix before route path when calling URLs [OK]
Common Mistakes:
Ignoring the router prefix when calling URLs
Using root path instead of mounted path
Confusing POST and GET URLs
5. You want to organize routes for a products resource using Express Router. You need to support listing all products, creating a product, getting a product by ID, updating a product by ID, and deleting a product by ID.
Which of the following router setups correctly implements all these routes following resource-based route organization?
Step 1: Understand resource-based route organization with routers
When using Express Router for a resource, routes inside the router use relative paths like '/', '/:id' because the router is mounted on the resource path (e.g., '/products').
Step 2: Identify the correct route paths
const router = express.Router();
router.get('/', ...);
router.post('/', ...);
router.get('/:id', ...);
router.put('/:id', ...);
router.delete('/:id', ...); uses relative paths inside the router, which is the correct pattern. The router would be mounted with app.use('/products', router).
Step 3: Compare with other options
const router = express.Router();
router.get('/products', ...);
router.post('/products', ...);
router.get('/products/:id', ...);
router.put('/products/:id', ...);
router.delete('/products/:id', ...); repeats the resource name in every route, which is redundant inside a router. The remaining options use non-standard paths that don't follow REST conventions.