0
0
Expressframework~30 mins

Resource-based URL design in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Resource-based URL Design with Express
📖 Scenario: You are building a simple web server for a library. The server will manage books and authors using resource-based URLs.
🎯 Goal: Create an Express server with routes that follow resource-based URL design principles for books and authors.
📋 What You'll Learn
Create an Express app instance called app
Define a route to get all books at /books
Define a route to get a single book by id at /books/:id
Define a route to get all authors at /authors
Define a route to get a single author by id at /authors/:id
💡 Why This Matters
🌍 Real World
Web servers often use resource-based URLs to organize data access clearly and predictably, like accessing books or authors by their IDs.
💼 Career
Understanding resource-based URL design and Express routing is essential for backend web development roles.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express by requiring the 'express' module. Then create an Express app instance called app by calling express().
Express
Need a hint?

Use require('express') to import Express and then call it to create the app.

2
Add configuration for port
Create a constant called PORT and set it to 3000.
Express
Need a hint?

Use const PORT = 3000; to set the port number.

3
Define resource-based routes
Add four routes to the app:
  • GET /books that sends 'List of books'
  • GET /books/:id that sends 'Book details for id: ' plus the id parameter
  • GET /authors that sends 'List of authors'
  • GET /authors/:id that sends 'Author details for id: ' plus the id parameter
Express
Need a hint?

Use app.get with the exact URL paths and send the specified text responses.

4
Start the server
Add code to start the server by calling app.listen with PORT and a callback that sends 'Server running on port ' plus the PORT.
Express
Need a hint?

Use app.listen(PORT, () => { ... }) to start the server and log the message.