app.route()
Registers a route using a full options object including method, schema, and handler.
Syntax
app.route(options: RouteOptions): FastifyInstance Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | RouteOptions | No | Includes method, url, schema, handler, preHandler, and hooks. |
Returns
FastifyInstance — The instance, for chaining.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.route({
method: 'GET',
url: '/users/:id',
schema: {
params: {
type: 'object',
properties: { id: { type: 'integer' } },
required: ['id'],
},
},
handler: async (req) => {
const { id } = req.params as { id: number };
return { id };
},
});
Notes
app.route() is the low-level form behind the shorthand methods. Use it
when you need to attach validation schemas, route-level hooks, or share a
config across multiple HTTP methods.