app.listen()

Starts the server, binding to a port and host after all plugins are ready.

Since Fastify 5 Spec ↗

Syntax

app.listen(options: { port, host? }): Promise<string>

Parameters

NameTypeRequiredDescription
options object No Object with port, optional host, and backlog.

Returns

Promise<string> — Resolves to the bound address.

Throws

  • Error — Rejected if the port is in use or plugins fail to load.

Examples

import Fastify from 'fastify';

const app = Fastify({ logger: true });

app.get('/', async () => ({ ok: true }));

try {
  await app.listen({ port: 3000, host: '0.0.0.0' });
} catch (err) {
  app.log.error(err);
  process.exit(1);
}

Notes

listen() implicitly calls ready(), so it resolves only after every plugin has booted. Bind to host '0.0.0.0' inside containers so the port is reachable externally.