Write the Endpoint Table

At the end of the last lesson, Dev’s desk card held five jobs, each with a method and a path, and one open question. When you asked curl for Priya’s ticket at /tickets/1, Express answered with its own HTML 404 page because no route matches yet. So how should your API answer each of those requests instead?

As a client, you’ve read whatever status and body an API sent back. In this lesson, you decide them. You’ll write the ticket API’s endpoint table before you write the remaining routes. It’s a short file in the project that gives every request its method, path, success status, and response body. Start with the paths, which have to work for every ticket at the desk.

Write Down the Paths

Dev’s desk card used /tickets/1, but the desk will look up Marcus’s lamp and Aiko’s bike too. The design needs one path that stands for any ticket, so the table writes the ID as a placeholder: /tickets/:id. The colon marks id as a name for whatever comes after the slash, so /tickets/1 and /tickets/5 both match /tickets/:id.

Express writes a route’s path the same way, which is why the table uses it. Reading the ID out of the path is the work of a later lesson. That’s all you need about :id for now.

A method and a path together make an endpoint, one kind of request the API answers. You write the endpoints down first because a row costs nothing to change, while a route that Dev’s desk already calls can’t change without breaking the desk. Create endpoints.md in the project folder, next to app.ts, and start it with the five rows from the desk card:

Language: Markdown Text Filename: endpoints.md
| Method | Path | Dev's job |
|---|---|---|
| GET | /tickets | List the tickets |
| POST | /tickets | Add a ticket |
| GET | /tickets/:id | Look up one ticket |
| PATCH | /tickets/:id | Change a ticket's status |
| DELETE | /tickets/:id | Delete a ticket logged by mistake |

The fourth row uses PATCH, which changes only the fields the request sends. Watch out for treating it as another name for PUT, which replaces the whole item with the body. A PUT with only a status asks the server to replace Priya’s ticket with one that has no name, item, or fault, and a careful server refuses it. Dev only ever changes a ticket’s status, so the table has a PATCH row and no PUT row.

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

Become a Member to join the conversation.