Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Design Paths Around Resources

In this lesson, you planned the five requests Dev’s sign-in desk needs before writing any of their routes. You picked a method and a path for each one, so the design is cheap to change while no code depends on it yet.

Key Points

  • A path names a thing, and the method names what to do with it. Your functions, getTickets() and addTicket(), are named for actions, but their routes are GET /tickets and POST /tickets. The action became the method, and the thing it works on became the path. That’s the core of a REST design, the style most web APIs you’ve called follow.
  • Paths named after functions repeat the method. A path like /getTickets or /deleteTicket says again what the method already says, and every new job needs a new path.
  • A collection has a plural name, and each item has a path of its own. /tickets names the whole collection. One ticket’s path is the collection’s path, a slash, then its id, like /tickets/1 for Priya’s toaster. The id doesn’t change, so the path stays the same when the ticket’s status does.
  • A change goes to the item, and adding goes to the collection. Putting the toaster on the bench changes a ticket that already exists, so it goes to /tickets/1. A new ticket has no id until the server gives it one, so POST goes to /tickets.
  • The item’s path takes three methods. GET reads the ticket, PATCH changes some of its fields, and DELETE removes it.
  • A GET must never delete anything. GET is a safe method: it asks for no change on the server. Prefetching browsers, search crawlers and link previews send GET requests on their own, so a deleting GET like GET /deleteTicket?id=1 would let them make tickets vanish.

Five jobs need only two paths, because the methods carry the actions.

The Design

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your ticket app can now list the café’s tickets and add a new one. Dev’s sign-in desk needs more. Dev wants to look up one ticket, change a ticket’s status when it goes onto the bench, and delete a ticket logged by mistake.

00:15 Before you write any of those routes, you’ll decide which method each request sends, and to which path. That’s designing an API, and a design costs far less to change before any code depends on it.

00:30 Start with the two jobs your app already does. In tickets.ts, their functions, getTickets() and addTicket(), are named for actions.

00:40 Compare each name with its route. The action became the method: getting the tickets is a GET request, and adding a ticket is a POST request, the method a client sends to create something.

00:52 The thing the action works on became the path, the tickets. So both routes share one path. The path names what the app holds, and the method says what to do with it.

01:04 That’s the core of a REST design, the style most web APIs you’ve called follow.

01:10 Watch out for paths named after functions, like /getTickets or /deleteTicket. They repeat what the method already says, and every new job needs a new path.

01:22 Some even delete with a GET request. That’s a separate mistake, and you’ll soon see why it’s dangerous.

01:30 The path /tickets names the whole collection, all five tickets at once. Its name is plural because it holds every ticket. Each ticket in it is an item, and an item gets a path of its own: the collection’s path, a slash, then the ticket’s id.

01:48 Priya’s toaster has the id 1, so /tickets/1 is the toaster’s own path.

01:55 The id names one ticket for as long as that ticket exists, so the item’s path stays the same, even when the ticket’s status changes.

02:04 Priya’s toaster has gone onto the bench, and Dev’s desk sends a request to record it. Your turn: pause the video and predict which of the two paths that request goes to.

02:20 It goes to /tickets/1, the item’s path. It changes a ticket that already exists, at its own path. A new ticket has no id until the server gives it one, so adding goes to the collection.

02:37 The item’s path takes three methods, one for each of Dev’s new jobs. GET reads Priya’s ticket. PATCH changes some of its fields, and Dev uses it to set the status to on the bench.

02:50 And DELETE would remove it, say if Dev had logged it by mistake. Look at the method on both paths. GET is a safe method, which means it asks for no change on the server.

03:03 So prefetching browsers, search crawlers and link previews send GET requests on their own. Let them find that deleting GET from earlier, and tickets vanish.

03:13 That’s why a GET must never delete a ticket. Dev’s five jobs need only two paths, and the methods carry the actions.

03:24 In a REST design, the path names a thing, the collection or one of its items, and the method names what to do with it.

03:33 Your app doesn’t know the item’s path yet. Ask curl for Priya’s ticket now, and Express answers with its own 404 page, because no route matches. So what should each of these five requests answer instead?

03:46 In the next lesson, Write the Endpoint Table, you’ll decide each one’s status code and response before you write a single route.

Become a Member to join the conversation.