Fastify
@dotpress/adapter-fastify runs your app on Fastify 5, on Node.js and AWS Lambda.
Options
Fastify plugins and hooks
Register Fastify plugins and hooks in beforeRoutes, which can be async: they apply to the Dotpress routes too.
The instance is ready once createApp() resolves: register everything in beforeRoutes or afterRoutes.
An existing instance
The app of the Fastify example mounts the Dotpress routes on an existing instance, next to a native route, with a Fastify hook applied to both:
Behavior
- Each Dotpress route is a native Fastify route. Its path (
request.routeOptions.url,/users/:id) shows in the Fastify logs, metrics and traces, and a native route with the same method and path fails at startup. Routing still follows the Dotpress rules: trailing slashes ignored,HEADonGETroutes,405withAllow. - Bodies are read by Dotpress, with the same rules on every adapter: the Fastify body parsers only apply to native routes, and
@fastify/multipartis not needed for uploads.preParsinghooks apply. Like Fastify, the adapter does not read the bodies ofGETandHEADrequests. - Errors with a
4xxstatusCoderaised by Fastify or its plugins (@fastify/rate-limit,@fastify/sensible…) use the Dotpress error format and keep their status and message. Other errors are unexpected (500). The adapter takes over the not-found and error handlers of the instance: they apply to native routes too. ctx.req.ipisrequest.ip: proxy headers are only used with thetrustProxyoption of Fastify, set when you create the instance.- Streamed responses (
stream(),sse()) are written directly to the Node.js response, so that their headers are sent at once:onSendhooks, such as@fastify/compress, do not apply to them. ctx.rawis the Fastify request.listen(port, host?)listens on every interface withouthost, like Node.js.close()closes the instance (itsonClosehooks), waits for pending requests and closes the streamed responses.
Testing with inject()
Besides the memory adapter, Fastify tests can use inject(), which goes through the Fastify hooks and plugins without opening a port:
On AWS Lambda
The same app runs on AWS Lambda with createLambdaHandler() of @dotpress/adapter-fastify/lambda: see AWS Lambda.