Build One Slice End to End

Niamh’s schedule API now has tests that send real requests and a reference that its own tests keep honest. Someone else wrote that server, though, and you came to it with every file in place. Gloria’s request line starts as an empty folder.

In this lesson, you’ll build the first resource of a new API from that empty folder. You’ll follow the same order for the rest of the request line: the reference first, then a skeleton every resource shares, then one resource through every layer, tests included. Start before any code, with what the API promises its clients.

Write the Reference Entry Before the Code

An empty folder leaves every decision open, and the reference is where you make them. Writing the entry first makes you choose the paths, fields, statuses, and error codes while changing them costs nothing. It also gives the code a definition of done to meet. That’s intent and acceptance applied to an API.

Every request Gloria takes is for a show, so shows come first. Here, a show is one broadcast of The Sunday Request Show on one date. Unlike the schedule’s weekly shows, each one happens once. Make the project folder with docs and routes folders inside:

Language: Shell
$ mkdir -p request-line/docs request-line/routes
$ cd request-line

Then write docs/api.md. Its Basics section repeats the schedule reference’s base URL and JSON format with Content-Type: application/json. It adds two lines: an X-Request-Id header on every response and the request form’s origin, http://localhost:5173. Below it come the parts the code will have to match:

Language: Markdown Text Filename: docs/api.md
## Errors

Every error has the same shape. `details` is `null` except for
`invalid_show`, where `details.errors` lists problems with the body
as a whole, such as an unknown field, and `details.properties`, when
present, has a key per field that broke a rule:

~~~json
{"error":{"code":"show_not_found",
"message":"No show has the id 99.","details":null}}
~~~

Each error has a status, a `code` and a `message`:

- `400` `invalid_json`: the body isn't valid JSON.
  `message`: The body isn't valid JSON.
- `404` `show_not_found`: no show has the id in the path.
  `message`: No show has the id `<id>`.
- `404` `route_not_found`: no endpoint has that method and path.
  `message`: No route for `<method> <path>`.
- `413` `invalid_body`: the body is over 100 KB.
  `message`: The server couldn't read the body.
- `415` `invalid_body`: the body isn't sent as `application/json`.
  `message`: Send a JSON body with Content-Type: application/json.
- `415` `invalid_body`: the `Content-Type` names a charset that isn't UTF.
  `message`: The server couldn't read the body.
- `422` `invalid_show`: the body breaks "The Show Object" rules.
  `message`: The show has invalid fields.
- `500` `server_error`: something failed on the server.
  `message`: Something went wrong on the server.

## The Show Object

| Field | Type | Rules |
|---|---|---|
| `id` | string | Set by the server |
| `title` | string | Required, not empty |
| `presenter` | string | Required, not empty |
| `date` | string | Required, a date as `YYYY-MM-DD` |

## GET /shows

Lists every show: `200` with an array of Show Objects, in the order
they were added.

~~~console
$ curl -i http://localhost:3000/shows
~~~

## GET /shows/:id

Gets the show whose `id` is `:id`, a string such as `1`: `200` with
the show, or `404` with `show_not_found`.

~~~console
$ curl -i http://localhost:3000/shows/1
$ curl -i http://localhost:3000/shows/99
~~~

## POST /shows

Adds a show. The body has `title`, `presenter` and `date`, and no
other fields:

~~~json
{"title": "The Sunday Request Show",
 "presenter": "Gloria Mendes", "date": "2026-12-20"}
~~~

Answers `201` with the new show and its path in `Location`, `422` with
`invalid_show`, `400` with `invalid_json`, or `415` with
`invalid_body`.

The statuses keep the schedule API’s rule of 400 for a body that isn’t JSON and 422 for JSON that breaks the show’s rules. The one new entry, route_not_found, answers an unknown path with the error shape, where the schedule API sent Express’s HTML page. That entry covers a wrong method on a real path too, such as PUT /shows/1.

As you saw with Niamh’s server, HTTP’s proper answer to a wrong method is 405 with an Allow header, but the request line keeps the one simpler rule for now.

Before any code exists, each entry says what to send, down to the path’s :id and a body to copy, and lists every answer the API can give. Every error’s code and message are in the list, because they’re your decisions, and the code will copy them from there. The full response examples wait, to be pasted from real runs as you did for Niamh’s reference.

The reference is the first layer of the shows resource. The others are a schema, a router with thin handlers, the data functions, its errors, and its tests. Building all of them for shows, before any second resource exists, is a slice. That’s the slice, beside the skeleton it plugs into:

The skeleton's createApp() mounts the shows slice (routes/shows.ts, shows.ts, store.ts). Its tests send requests to createApp(), and the slice's files throw to the error handler.
One Resource Through Every Layer, on a Shared Skeleton

You’ll build the skeleton box first, then the slice’s files from the store up to the tests, and then check the running server against docs/api.md.

Set Up the Skeleton Once

Every resource needs the same frame around it, the diagram’s skeleton box. It reads settings from .env, gives each request an id and a log line, allows CORS for the request form, parses JSON bodies, and sends every error from one place.

Build that frame once, before any route, so the first route has all of it from the start. In the request-line folder, create the package.json and install the packages:

Language: Shell
$ npm init -y
...
$ npm pkg set type=module
$ npm install express zod cors
...
$ npm install --save-dev typescript @types/node @types/express @types/cors
...
$ npm pkg set scripts.start="node --env-file=.env server.ts"
$ npm pkg set scripts.test="node --test"
$ npm pkg set scripts.check="tsc --noEmit"

@types/cors adds the types that cors ships without. Use the schedule project’s tsconfig.json unchanged. The scripts run your .ts files directly, as Node 26 does without a build step.

Language: JSON Filename: tsconfig.json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "strict": true,
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "types": ["node"]
  }
}

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.