Menu

Routing and request handling — Fastify with TypeScript (part 5/18)
📰

Routing and request handling — Fastify with TypeScript (part 5/18)

DEV Community: fastify·Antonio Tripodi·11 days ago
#qt3h4IYH
#devtofastify#devto#dev#users#route
Long article (1689 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.