Skip to main content
Sonamu is a high-performance HTTP server based on Fastify. You can configure server port, host, plugins, lifecycle hooks, and more.

Basic Structure

baseUrl

Specifies the full URL where the server is externally accessible. Type: string (optional) Default: http://{listen.host}:{listen.port}
Using actual domain:
baseUrl is used for URLs generated during file uploads, SSR meta tags, etc.

listen

Configures the port and host the server listens on. Type: (optional)

port

The port number the server listens on. Type: number
Setting port via environment variable:

host

The host address the server binds to. Type: string (optional) Default: "localhost"
Listen on all network interfaces:
When using 0.0.0.0 in production, verify your firewall settings.

fastify

Configures Fastify server options. Type: Omit<FastifyServerOptions, "logger"> (optional)
Logging is configured separately through Sonamu’s logging settings.

plugins

Enables and configures Fastify plugins.

formbody

Parses application/x-www-form-urlencoded request bodies. Type: boolean | FastifyFormbodyOptions (optional)
With options:

qs

Parses query strings. Supports nested objects and arrays. Type: boolean | QsPluginOptions (optional)

multipart

Handles file uploads (multipart/form-data). Type: boolean | FastifyMultipartOptions (optional)
Allowing larger files:

static

Serves static files. Type: boolean | FastifyStaticOptions (optional)
Example: /api/public/images/logo.pngpublic/images/logo.png file

session

Enables session management. Type: boolean | SecureSessionPluginOptions (optional)
In production environments, you must set secret and salt via environment variables!
Production settings:

compress

Compresses responses. See separate documentation for details. Type: boolean | FastifyCompressOptions (optional)
compress detailed settings

cors

Configures CORS (Cross-Origin Resource Sharing). Type: boolean | FastifyCorsOptions (optional)
Allow all origins in development:

sse

Supports Server-Sent Events. Type: boolean | SsePluginOptions (optional)
SSE usage

custom

Registers custom Fastify plugins. Type: (server: FastifyInstance) => void (optional)

apiConfig

Configures API behavior.

contextProvider

A function that creates Context for each API call. Type: (defaultContext, request) => Context
Context detailed explanation

guardHandler

A function called when Guard decorators execute. Type: (guard, request, api) => void | Promise<void>

cacheControlHandler

A function that sets Cache-Control headers. Type: (req) => string | undefined
Cache-Control detailed explanation

lifecycle

Registers hooks for server lifecycle events.

onStart

Executes when the server starts. Type: (server: FastifyInstance) => void | Promise<void>

onShutdown

Executes when the server shuts down (graceful shutdown). Type: (server: FastifyInstance) => void | Promise<void>

onError

Executes when an unhandled error occurs. Type: (error, request, reply) => void | Promise<void>

Practical Examples

Basic Development Server

Production Server

Docker Container Environment

Next Steps

After completing basic server settings:
  • auth - Authentication settings
  • storage - File storage settings
  • cache - Cache settings
  • compress - Response compression settings