Fastify
@navios/adapter-fastify runs a Navios application on Fastify 5, on Node.js. The adapter registers the application's routes with Fastify, turns each Fastify request into the input of a compiled route and writes the result. Guards, validation and error mapping happen in @navios/server and behave the same as on every other adapter; Fastify's own schema validation and serialisation are not used.
Install and use
@fastify/cors and @fastify/multipart are dependencies of the adapter and do not need to be installed separately.
app.listen() takes Fastify's listen options and resolves to the server's address. It needs a port, or the path of a unix socket. Without a host, Fastify listens on localhost, on each of its addresses. app.getServer() returns the FastifyInstance once the application is initialised.
In tests, app.inject() goes through fastify.inject(), so Fastify's parsing, hooks and plugins take part without opening a socket. See Testing.
Options
defineFastifyEnvironment(options) accepts the options of the fastify() factory for an HTTP/1.1 server, such as logger, bodyLimit, trustProxy and https, and three of its own. All are optional. Fastify's http2 is not supported.
configure runs after the CORS and multipart plugins have been registered. Plugins that it registers are loaded before the adapter goes on, whether or not it awaits them. A plugin can reach the same Fastify instance when the setup belongs in a reusable package.
The Register declaration
Write this once in the application, usually next to NaviosFactory.create. It types ctx.raw, app.getServer() and the options of app.listen() for Fastify. Request context explains the mechanism and what happens without the declaration.
What ctx.raw contains
On this adapter ctx.id is Fastify's request.id, and ctx.signal aborts when the connection closes before the response has been written completely. ctx.ip and the protocol and host of ctx.url are Fastify's request.ip, request.protocol and request.host, so the trustProxy option applies to them. In a test request without a remoteAddress, request.ip is the 127.0.0.1 of Fastify's own inject(), and ctx.ip is undefined.
Code that uses ctx.raw runs on this adapter only. Prefer the adapter-neutral members of the context (ctx.ip, ctx.headers, ctx.cookies, ctx.reply.header(), ctx.signal) where they cover the need.
Taking over the response
A handler may write the response itself through the native reply. When reply.sent is true after the handler returns, which is the case after reply.send() or reply.hijack(), the adapter writes nothing.
- Headers and cookies set with
ctx.replyare not sent on a response that was taken over. Set them on the native reply. - The pipeline skips response validation and ignores the handler's return value, whatever the endpoint declares.
- The request is released when that response has ended, so
ctx.use()andctx.onFinish()work until then.ctx.responseStatusis the status the handler wrote. - A handler that throws after
reply.hijack(), before it wrote anything, closes the connection. One that returns may write later, with any status.
Use this for cases the framework does not cover. For ordinary streaming, return a ReadableStream or an async iterable from an endpoint declared with response: 'stream', as described in Streams and server-sent events; that code also runs on Bun.
CORS
cors is passed to @fastify/cors unchanged. See that plugin's documentation for the options. Set methods: by default the plugin allows a preflight request the methods GET, HEAD and POST, so that browsers refuse to send a PUT or a DELETE.
Every response carries the headers, including those of errors and of URLs that the router rejects. A Vary header that a handler sets is sent next to the one of the plugin.
Multipart
Enable @fastify/multipart with multipart: true, or with its options to set limits:
An endpoint declared with body: 'multipart' then receives text fields as strings and files as File objects, validated by the endpoint's requestSchema. File uploads covers the handler side.
- All parts are read into memory before the handler runs. Together they may have as many bytes as
bodyLimit, and whatlimitsallow on top:fileSizefor each offiles, andfieldSizefor each offields, one of each when the number is not set. A body over that is answered with 413. So setlimits.fileSizefor files larger thanbodyLimit, andlimits.filesto accept several of them. - A request over any of the
limits(fileSize,files,fields,parts,fieldSize,fieldNameSize) is answered with a 413 problem response, and the handler does not run. That includes a text field longer thanfieldSize, which is rejected rather than truncated. - The name of a file is never a path:
../../etc/passwdarrives aspasswd. A part without a name is left out. - Without the
multipartoption, an endpoint declared withbody: 'multipart'makesinit()throw an error that names the route, unlessconfigureregisters@fastify/multipartitself. - The adapter collects the parts itself, so the options of the plugin that do that (
attachFieldsToBody,onFile,sharedSchemaId) are not accepted.
Request bodies
- Fastify collects a body of any type as bytes, before the handler and its guards, and the server decodes it by its content type when the pipeline asks for it. A multipart body is not collected before that.
- Fastify hooks see the undecoded
Bufferinrequest.body. A hook may set another body, which is then taken as it is. - A content type parser that
configureadds keeps its content type: what it makes of a body goes to validation as it is, andctx.rawBody()is not available for it. bodyLimit(1 MiB by default, a Fastify option) is enforced while the body is collected.- When a request has two
Content-Typeheaders, Node keeps the first.
Errors outside the pipeline
Errors that Fastify, a plugin or a hook raises are answered as application/problem+json, like the errors described in Errors. Error mappers do not apply to them, because there is no route.
Pass frameworkErrors or clientErrorHandler, which are Fastify options, to answer the last row yourself.
Shutdown
app.close() calls fastify.close() and waits for it, for at most the application's shutdownTimeout (10 seconds by default). Fastify stops accepting connections and lets in-flight requests finish. The adapter does not wait for keep-alive connections: responses sent while the server closes carry connection: close, and connections that become idle, or that never sent a request, are dropped.
When the timeout passes, the adapter closes the remaining connections, including those that were upgraded, such as WebSockets. That aborts the ctx.signal of requests that are still running, and the application continues with its shutdown hooks. A plugin that keeps connections of its own, such as @fastify/websocket, closes them earlier, in its preClose hook. Application setup describes the whole shutdown sequence.
Listening on localhost opens one socket per address, and only the connections of the first are known to the adapter. In production, pass a host such as 0.0.0.0.
Requirements
- Node.js 24 or newer
fastify5.6 or newer (peer dependency)@navios/serverof the same version (peer dependency), with@navios/builder