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.

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"))

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