0Pricing
TypeScript Academy · Lesson

Router handlers, Request/Response typing

Type Express route handlers: params, query, and JSON bodies; send typed responses with res.json and reusable DTOs.

Router handlers, Request/Response typing is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Goal: Wire a small Express API with strict types. You will type route params, query, and JSON bodies, then return typed JSON with res.json.

  • DTO interfaces
  • Request<P, Res, Body, Q>
  • Typed res.json

Server setup

Initialize Express and enable JSON parsing. Keep the health endpoint small and typed with Request/Response.

import express, { Request, Response } from "express"

const app = express()
app.use(express.json())

app.get("/", (_req: Request, res: Response) => {
  res.type("text/plain").send("OK")
})

app.listen(3000, () => console.log("http://localhost:3000"))

Params & query

Type each slot explicitly: Params, Query, and ResBody. Autocomplete helps avoid typos.

import { Request, Response } from "express"

type Params = { id: string }
 type Query = { verbose?: "1" | "0" }
 type ResBody = { id: string; name: string; details?: string }

export function getUser(req: Request<Params, ResBody, unknown, Query>, res: Response<ResBody>) {
  const { id } = req.params
  const { verbose } = req.query
  const body: ResBody = { id, name: "Ada", details: verbose === "1" ? "Loves math" : undefined }
  return res.status(200).json(body)
}

Typed body & res

Annotate the body for POST/PUT, plus a typed response. Your DTOs double as documentation.

import { Request, Response } from "express"

type CreateUserBody = { name: string }
 type CreateUserRes = { id: string; name: string }

export function createUser(req: Request<{}, CreateUserRes, CreateUserBody>, res: Response<CreateUserRes>) {
  const { name } = req.body
  const result: CreateUserRes = { id: "u_1", name }
  return res.status(201).json(result)
}

Wire routes

Compose small, typed handlers. Route params are validated by matching the template (e.g., :id).

import express from "express"
import { getUser } from "./getUser"
import { createUser } from "./createUser"

const app = express()
app.use(express.json())

app.get("/users/:id", getUser)
app.post("/users", createUser)

app.listen(3000, () => console.log("http://localhost:3000"))

Tips

Tips:

  • Keep DTOs small and reuse across handlers.
  • Prefer union literals for constrained query values.
  • Return precise status codes and typed JSON.

Express typing check

Quick check: How do you type an Express handler with params and JSON body?

Recap

Recap: You typed route params, query, bodies, and responses. The same pattern scales to routers and middlewares.

Frequently asked questions

Is the “Router handlers, Request/Response typing” lesson free?

Yes — the full text of “Router handlers, Request/Response typing” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Router handlers, Request/Response typing”?

Type Express route handlers: params, query, and JSON bodies; send typed responses with res.json and reusable DTOs. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Router handlers, Request/Response typing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Router handlers, Request/Response typing
  2. Schema validation with zod & inference to TS
  3. Error middleware & result types
← Back to TypeScript Academy