Application setup
An application is created from a root module and an adapter, then initialised and started.
NaviosFactory.create(rootModule, options) only constructs the application. Nothing is resolved and no server exists until init() or listen().
Options
Keep validateResponses at 'always' unless a profile shows it matters: validation is what removes undeclared fields from responses. There is no setting that validates in development only, because it would send in production what development removes.
Settings of the HTTP server itself, such as CORS, body limits and multipart, are options of the adapter factory, not of the application. See Fastify and Bun.
Methods
usePlugin, intercept, mapError and enableShutdownHooks return the application, so calls can be chained.
The options of listen belong to the adapter: { port, host } on Fastify, { port, hostname } on Bun.
What init() does
In order:
- Walks the module graph from the root module and registers module providers, so that everything resolved afterwards, the logger included, sees them.
- Enables the async context when
asyncContextis on. - Resolves the four error responders and applies the mappers registered so far.
- Creates the adapter.
- Runs each plugin's set-up, in dependency order.
- Walks the module graph again when plugins added modules, and registers the providers of those modules.
- Sets up the logger, which a plugin or a module may provide, and writes what was logged before this point.
- Instantiates each module and runs
onModuleInit, imported modules first. - Instantiates controllers, guards and interceptors, and compiles one function per route. Broken route rules are reported here.
- Registers the routes with the adapter and waits for it to be ready.
- Runs plugin bootstrap hooks, then
onApplicationBootstraphooks.
An error in any step rejects the promise returned by init(), and therefore the one returned by listen(). Before it does, whatever was built is shut down, as by close() but without waiting for work in flight, so that nothing is left running. The same happens when a transport fails to start. The container is disposed then; an application that failed stays failed.
A hook that init(), start() or close() waits for cannot wait for that call in turn. await app.start() in onModuleInit rejects and says so; app.start() or app.close() there, without await, starts or closes the application once it is initialised. await app.close() in a shutdown hook resolves at once.
Bootstrap and shutdown hooks
Any singleton service may implement these:
signal is the signal name when the shutdown came from enableShutdownHooks. The per-service hooks onServiceInit and onServiceDestroy belong to DI and are described in Lifecycle.
The hooks are called on the instances the container holds at that moment. A service that nothing has resolved by the end of init(), for example one that is only resolved lazily inside a handler, does not get onApplicationBootstrap. Inject such a service into a module, a controller or another eagerly created singleton if it needs the hook. Objects the container did not build, from provide(token, { value }) or addInstance(), do not get the hooks at all, and neither does one that does not declare the hook itself or through its class, such as a proxy that answers for every name.
Shutdown
app.close() does the following:
- Asks the adapter to stop accepting connections and to wait for in-flight requests, for at most
shutdownTimeoutmilliseconds. Then everything that requests still hold is released: streams are closed,onFinishcallbacks run and request scopes are disposed. - Runs
onApplicationShutdownhooks, dependents before what they use. - Runs plugin shutdown hooks in reverse registration order.
- Disposes the container, which runs
onServiceDestroy, dependents first as well.
When the application is still initialising or starting, close() waits for that first. Every step gets shutdownTimeout, each hook on its own: what takes longer is logged with its name and left behind, so a hook or a database that hangs does not keep the process. An error in a step is logged and does not stop the remaining ones. Calling close() again returns the shutdown in flight. A closed application cannot be started again; create a new one.
To shut down when the process is asked to stop, opt in:
On the first of the given signals the application calls close(signal). This is opt-in because a library should not install process signal handlers on its own. Call it before init() or listen(), so that a signal during the start-up is seen.
A signal that has a listener no longer ends the process. When every application the signal closes is closed, the process is therefore sent the signal again and ends the way it would have without Navios, with exit code 143 after SIGTERM, also when a timer or a socket is left. As process 1, which a container's main process is, it exits with that code by itself. It is not when something else in the process listens to the signal, or with app.enableShutdownHooks(['SIGTERM'], { exit: false }). A second signal ends the process at once. A signal during an app.close() that your code called waits for it.
The application also supports explicit resource management, which closes it when the variable goes out of scope: