Bird
Raised Fist0
Expressframework~5 mins

Resource-based URL design in Express - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is resource-based URL design in web development?
It is a way to organize URLs so each URL represents a resource (like users or products) instead of actions. This makes URLs simple and easy to understand, like /users or /products/123.
Click to reveal answer
beginner
Why use nouns instead of verbs in resource-based URLs?
Nouns represent resources (things) and verbs represent actions. Using nouns keeps URLs consistent and lets HTTP methods (GET, POST, PUT, DELETE) define the action clearly.
Click to reveal answer
beginner
Example: What HTTP method and URL would you use to update a user with ID 5?
Use PUT /users/5. PUT means update, and /users/5 points to the user resource with ID 5.
Click to reveal answer
intermediate
How does Express help implement resource-based URL design?
Express lets you define routes with URL patterns and HTTP methods. For example, app.get('/users', ...) handles getting all users, and app.post('/users', ...) handles creating a user.
Click to reveal answer
beginner
What is the benefit of using resource-based URLs for API clients?
Clients can predict URLs and actions easily. They know to use GET to read, POST to create, PUT to update, and DELETE to remove resources, making APIs easier to use and understand.
Click to reveal answer
Which URL best follows resource-based design for accessing a list of books?
A/books/list
B/getBooks
C/books
D/fetchBooks
Which HTTP method is used to delete a resource in resource-based URL design?
ADELETE
BPOST
CGET
DPUT
In Express, how do you define a route to update a user with ID parameter?
Aapp.put('/users/:id', ...)
Bapp.delete('/users/:id', ...)
Capp.post('/users/:id', ...)
Dapp.get('/users/:id', ...)
What does the URL /products/123 represent in resource-based design?
AA search for products
BA list of products
CAn action to create a product
DA product with ID 123
Why avoid verbs in resource-based URLs?
ABecause verbs are not allowed in URLs
BBecause HTTP methods already define actions
CBecause verbs are harder to type
DBecause verbs slow down the server
Explain how resource-based URL design organizes URLs and HTTP methods in an Express app.
Think about how URLs and HTTP verbs work together.
You got /4 concepts.
    Describe the benefits of using resource-based URL design for API users and developers.
    Consider how it helps both sides communicate.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main idea behind resource-based URL design in Express?
      easy
      A. Combine all actions into one URL
      B. Use random URLs for each action
      C. Avoid using HTTP methods in URLs
      D. Organize URLs around data items like users or books

      Solution

      1. Step 1: Understand resource-based URL design

        This design organizes URLs by resources such as users or books, making them clear and meaningful.
      2. Step 2: Compare options

        Options B, C, and D do not follow this clear organization principle.
      3. Final Answer:

        Organize URLs around data items like users or books -> Option D
      4. Quick 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
      A. app.get('/user/:id', handler)
      B. app.get('/getUser', handler)
      C. app.post('/user/:id', handler)
      D. app.delete('/user', handler)

      Solution

      1. 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'.
      2. Step 2: Check options

        app.get('/user/:id', handler) uses GET and '/user/:id' which is correct. Others use wrong methods or URLs.
      3. Final Answer:

        app.get('/user/:id', handler) -> Option A
      4. Quick 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
      A. 404 Not Found error
      B. "Deleted book :bookId"
      C. "Deleted book 123"
      D. SyntaxError

      Solution

      1. 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.
      2. Step 2: Check response behavior

        The handler sends a string with the bookId inserted, so response is 'Deleted book 123'.
      3. Final Answer:

        "Deleted book 123" -> Option C
      4. Quick 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
      A. req.params.id should be req.body.id
      B. Missing :id parameter in URL path
      C. res.send should be res.json
      D. Using PUT instead of POST

      Solution

      1. 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.
      2. Step 2: Identify mismatch

        Since ':id' is missing in URL, req.params.id will be undefined causing error or wrong behavior.
      3. Final Answer:

        Missing :id parameter in URL path -> Option B
      4. Quick 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
      A. app.put('/posts/10/comments/45', handler)
      B. app.put('/updateComment', handler)
      C. app.put('/comments/45', handler)
      D. app.put('/posts/comments', handler)

      Solution

      1. Step 1: Understand nested resource URLs

        Comments belong to posts, so URL should reflect this hierarchy: '/posts/:postId/comments/:commentId'.
      2. 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.
      3. Final Answer:

        app.put('/posts/10/comments/45', handler) -> Option A
      4. Quick 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