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()andaddTicket(), are named for actions, but their routes areGET /ticketsandPOST /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
/getTicketsor/deleteTicketsays 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.
/ticketsnames the whole collection. One ticket’s path is the collection’s path, a slash, then its id, like/tickets/1for 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, soPOSTgoes to/tickets. - The item’s path takes three methods.
GETreads the ticket,PATCHchanges some of its fields, andDELETEremoves it. - A
GETmust never delete anything.GETis a safe method: it asks for no change on the server. Prefetching browsers, search crawlers and link previews sendGETrequests on their own, so a deletingGETlikeGET /deleteTicket?id=1would let them make tickets vanish.
Five jobs need only two paths, because the methods carry the actions.