Menu

1 / 2

Fastify Has a Free API You Should Know About

DEV Community: fastify·Alex Spinov·5 months ago
#iSiqfe8P
#dev#request#fastify#type#article
Reading 0:00
15s threshold

Fastify is one of the fastest Node.js web frameworks — but its plugin system, schema validation, and hooks make it far more powerful than a simple HTTP server.

Plugin Architecture

Fastify's encapsulated plugin system is its killer feature:

import Fastify from " fastify "

const app = Fastify ({ logger : true })

// Register a plugin with encapsulated scope app . register ( async function userRoutes ( fastify ) { // This decorator is only visible inside this plugin fastify . decorate ( " userService " , new UserService ())

fastify . get ( " /users " , async ( request ) => { return fastify . userService . findAll ( request . query ) })

fastify . get ( " /users/:id " , async ( request ) => { return fastify . userService . findById ( request . params . id ) })

fastify . post ( " /users " , async ( request , reply ) => { const user = await fastify . userService . create ( request . body ) reply . code ( 201 ). send ( user ) }) }, { prefix : " /api/v1 " })

await app .…

Continue reading — create a free account

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