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
- Router handlers, Request/Response typing
- Schema validation with zod & inference to TS
- Error middleware & result types