Skip to content

onShutdown() & close()

app.onShutdown(fn)
NameTypeDescription
fnFunctionCleanup function (sync or async).

Returns: app (for chaining).

app.onShutdown(async () => {
await db.disconnect();
console.log("Database closed");
});
// Chaining
app
.onShutdown(() => db.disconnect())
.onShutdown(() => cache.close())
.start(3000);

Hooks run sequentially in registration order. If one throws, remaining hooks still execute.


// Promise API
await app.close();
// Callback API
app.close(callback);
NameTypeDescription
callbackFunctionOptional. Called with (err) when shutdown completes.

Returns:

  • Without callback: Promise<void>
  • With callback: app
  1. Stops the server from accepting new connections
  2. Drains existing requests (up to configured timeout)
  3. Runs all registered onShutdown hooks
  4. Resolves / calls callback

Safe to call multiple times — second call resolves immediately (idempotent).

// Programmatic shutdown in tests
afterAll(async () => {
await app.close();
});
// Callback style
app.close((err) => {
if (err) console.error("Shutdown error:", err);
process.exit(err ? 1 : 0);
});