app.setNotFoundHandler()
Defines a custom handler for requests that match no route.
Syntax
app.setNotFoundHandler(options?, (request, reply) => any) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | object | No | Optional preValidation and preHandler hooks. |
handler | Function | No | Receives the request and reply for unmatched routes. |
Returns
FastifyInstance — The instance, for chaining.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.setNotFoundHandler((req, reply) => {
reply.code(404).send({
error: 'Not Found',
path: req.url,
});
});
Notes
Only one not-found handler exists per encapsulated context. Define it
inside a plugin to customize 404 responses for a specific route prefix.