Routing and request handling
Fifth chapter of the "Fastify with TypeScript" series. With app.ts and server.ts in place, it's time to build our first real routes. By the end of the chapter the project will have a users module that can create, read and list users, and we'll have met the three places a request carries data: route parameters, the query string and the body.
A module for the users
Each area of the application gets its own folder under src/ . Create src/users/users.route.ts :
// src/users/users.route.ts import type { FastifyInstance } from 'fastify' ;
export default async function userRoutes ( fastify : FastifyInstance ) { fastify . get ( '/' , async ( ) => { return [ ] ; } ) ; }
Enter fullscreen mode
Exit fullscreen mode
This file default-exports a plugin : an async function that receives the Fastify instance and declares routes on it. To use it, register it in src/app.ts .…