Server platform
The server platform is the Node.js service that makes one Trilium document available to browser, desktop, mobile, sync, and automation clients. It prepares the shared product core, supplies host services, composes the client-facing application, and keeps persistence and client updates behind clear boundaries.
System boundary
The server joins clients to shared note behavior and durable state. It receives a request or background task, applies the shared product contract, stores approved changes, and returns a response or live event without making any client the source of truth.
This diagram shows the platform boundary and the main direction of state flow.
flowchart LR
Client[Browser, desktop, mobile, or automation client]
App[Node server application]
Core[Shared product core]
Providers[Runtime providers]
Document[(SQLite document)]
Events[HTTP responses and live events]
Client --> App --> Core
Core --> Providers
Providers --> Document
Core --> Events --> Client
Deployment boundary
The same server capability runs as a dedicated service or inside the desktop application. A dedicated service supports browser and mobile use as well as synchronization across devices. The embedded service runs only while the desktop application runs.
The desktop server is disabled for network use by default. When network access is off, the web application and published notes are disabled, while ETAPI, MCP, and supported local integrations remain available on the local device. Enabling network access allows the web interface and sharing surface to answer on the local network.
Desktop network access limits which web-facing services can answer beyond the local device. A reverse proxy can provide the public entry point and TLS. It must preserve the client address rules and live connection upgrades.
References: apps/…/help_content.json• L4 apps/…/help_content.json• L7 apps/…/help_content.json• L10-18 apps/…/help_content.json• L21-24
Startup lifecycle
Startup makes the document and its host services ready before it exposes normal client work. It restores an interrupted database change when needed, opens the document, initializes the shared core, builds the application, binds the listener, attaches supported live messaging, and then marks the service ready.
The startup sequence has these stages:
- Load configuration and resolve the data and resource locations.
- Recover an interrupted document restore.
- Open the SQLite document in the selected read-write or read-only mode.
- Register Node providers and platform state with the shared core.
- Build the web application and bind HTTP, HTTPS, or the selected local socket.
- Attach live messaging only when the provider supports an HTTP connection.
- Register OCR and server intelligence extensions.
- Start normal background work and publish readiness.
Setup state can keep normal document access closed while the first-run wizard or a restore replaces the document. A failure before readiness stops the process. A later background failure is contained and reported so that normal note access can continue.
References: apps/…/main.ts• L12-25 apps/…/core_init.ts• L31-102 apps/…/www.ts• L27-94 apps/…/routes.ts• L59-74 apps/…/process_errors.ts• L14-23
Application composition
The application composition layer turns the prepared core into a client-facing service. Middleware establishes request context and security behavior before the server selects static resources, setup, authentication, shared APIs, integrations, publishing, or feature services.
Request surface
The HTTP application serves the web client and routes requests to the product boundaries. It applies cross-origin rules, security headers, request parsing, compression where safe, session handling, and the desktop network gate before the main route groups.
The bootstrap response tells each client whether the document is initialized and whether the request is a trusted desktop renderer. It supplies the matching setup, login, or signed-in application state. Browsers that reach a desktop listener do not inherit the desktop renderer's trust.
References: apps/…/app.ts• L29-35 apps/…/app.ts• L37-107 apps/…/app.ts• L115-134 apps/…/index.ts• L20-109 apps/…/index.ts• L113-193
Route composition
The route registry connects server-only paths with shared route builders. Shared builders keep common note contracts in the core. Node routes add host-dependent work such as network discovery, metrics, OCR, media access, imports, resources, and local database operations.
The registry also selects the trust path for each client. Browser sessions use session and CSRF checks. ETAPI and integrations use tokens where a browser session is not suitable. MCP uses its own token guard. Desktop-only access uses the trusted renderer or local extension origin.
References: apps/…/routes.ts• L48-110 apps/…/routes.ts• L118-176 apps/…/routes.ts• L182-220 apps/…/route_api.ts• L31-48 apps/…/route_api.ts• L71-121
Authentication boundary
Authentication connects a client to protected HTTP and live access. Local password login can include a time-based one-time password. OpenID Connect delegates sign-in to an external identity provider. Sync and token clients use separate credential checks because they do not follow the browser login flow.
All credential paths share a failed-login limit. A successful login creates a fresh session. A desktop browser request still follows the normal web trust path even when the server runs inside the desktop application.
References: apps/…/login.ts• L13-21 apps/…/login.ts• L57-92 apps/…/login.ts• L125-141 apps/…/login.ts• L16-67 apps/…/login.ts• L69-83
Live client messaging
Live messaging carries committed entity changes to connected clients so they do not need to poll for every update. The server attaches a WebSocket endpoint only when the active messaging provider can use the HTTP listener. A desktop host may use another transport and therefore does not require a WebSocket endpoint.
References: apps/…/core_init.ts• L51-61 apps/…/www.ts• L73-86 apps/…/ws_messaging_provider.ts• L13-25 apps/…/ws_messaging_provider.ts• L39-109
Operational interfaces
The platform exposes a health result for service checks, metrics for monitoring, and network addresses for setup and synchronization screens. CPU architecture checks run in the background so startup does not wait for a host command that is not needed to serve the first page.
References: apps/…/routes.ts• L123-131 apps/…/metrics.ts• L8-20 apps/…/system_info.ts• L20-33 apps/…/system_info.ts• L41-75 apps/…/routes.ts• L218-220
Runtime providers
Runtime providers translate host capabilities into contracts that the shared core can use. This lets shared product behavior stay independent of Node, Electron, file paths, native libraries, sockets, and operating-system APIs.
| Provider role | Shared capability |
|---|---|
| Platform and context | Host details, paths, process control, and request-scoped state |
| Database | SQLite access, transactions, document replacement, and close behavior |
| Request and cryptography | Controlled network access and native cryptographic work |
| Messaging | Live client events and client messages |
| Backup, archive, and image services | Durable transfer, export formats, and media preparation |
| Feature services | Logging, help, OCR, resources, and server-side intelligence |
The server passes these providers to the core during initialization. Child pages cover the persistence services in Server storage and note services, the HTTP and publishing surface in Server HTTP API and sharing, and server-side intelligence in Server intelligence and automation.
References: apps/…/core_init.ts• L6-23 apps/…/core_init.ts• L39-102 packages/…/index.ts• L179-244 packages/…/types.ts• L21-53 packages/…/types.ts• L21-67
Configuration boundary
Configuration defines how the service binds to its host and which boundaries it exposes. Environment values override config.ini, and built-in defaults provide the last value. The settings cover the listener, HTTPS, proxy trust, sessions, network access, synchronization, backups, read-only mode, and server features.
Most settings take effect at startup. This gives long-running services and containers a stable operating mode.
References: apps/…/config.ts• L1-20 apps/…/config.ts• L224-265 apps/…/config.ts• L587-635 apps/…/www.ts• L123-159 apps/…/www.ts• L165-210
Data and resource boundary
The data directory is the durable home for the document and server state. It also supports configuration, logs, backups, temporary work, caches, and downloaded feature resources. Application assets remain read-only inputs, while managed resources stay with the data so an application update does not replace them.
Managed resources become usable only after the downloaded files pass their declared size and SHA-256 checks. This keeps optional feature files separate from the document while making their state visible to the application.
References: apps/…/data_dir.ts• L16-112 apps/…/resource_dir.ts• L4-19 apps/…/resources.ts• L120-215 apps/…/resources.ts• L221-305 apps/…/resources.ts• L7-41
Outbound request boundary
The request provider is the controlled path from server features to other HTTP services. It uses a strict policy for URLs found in notes. It uses a separate policy for operator-configured service endpoints because those endpoints have a different trust level.
Note-linked requests reject private or reserved addresses, keep the connection on the checked address, limit redirects, and bound response data. Configured endpoints can use approved private services but do not follow redirects while carrying credentials.
References: apps/…/request.ts• L204-237 apps/…/request.ts• L241-311 apps/…/safe_fetch.ts• L37-55 apps/…/safe_fetch.ts• L74-107 apps/…/safe_fetch.ts• L212-277
Host work boundary
Large or host-sensitive work stays behind controlled services. Image work can use bounded workers. Imports use temporary files and progress reporting. OCR adds derived text through the document model. External agents and model tools run outside the main server request path and report failure to the client instead of taking down the service.
These features use the same providers and request context as normal note work. Their product behavior and operational interfaces are described in Server intelligence and automation.
References: apps/…/image_worker_pool.ts• L1-25 apps/…/image_worker_pool.ts• L64-149 apps/…/importer.ts• L114-242 apps/…/ocr_service.ts• L108-121 apps/…/claude_agent.ts• L282-405
Persistence and client boundaries
The server keeps one durable document behind the shared core. A client request becomes shared state inside a transaction, the database commits that state, and the resulting entity changes return to clients through HTTP or live messaging.
Persistence boundary
The database provider owns the native SQLite connection and file lifecycle. The shared core owns note rules, queries, transactions, migrations, and its in-memory view of the document. A successful commit publishes entity changes. A rollback restores the in-memory view when a failed transaction changed tracked state.
Database initialization, restore, backups, and note services are covered in Server storage and note services.
References: apps/…/sql_provider.ts• L9-89 apps/…/core_init.ts• L39-75 packages/…/types.ts• L21-53 apps/…/route_api.ts• L87-103
Client transport boundary
HTTP carries page loads, authentication, API calls, setup, uploads, downloads, integrations, publishing, and feature services. Live messaging carries committed changes after the client passes its connection check. The desktop renderer can use the same application surface through its trusted in-process path.
Large imports and share-target files use temporary transfer state instead of requiring the whole request to remain in memory. The server owns cleanup when a request closes or a process ends.
References: apps/…/routes.ts• L54-74 apps/…/routes.ts• L93-110 apps/…/routes.ts• L167-187 apps/…/route_api.ts• L199-212 apps/…/route_api.ts• L222-263 apps/…/route_api.ts• L331-365
Published content boundary
Published notes provide a read-focused surface for people who do not use the main application. The server checks share access, reads through a restricted view, renders the selected content, and applies visibility rules before returning it. Public search uses the same access rules as direct reads.
HTTP APIs and publishing details are covered in Server HTTP API and sharing.
References: apps/…/share_provider.ts• L12-51 apps/…/routes.ts• L13-100 apps/…/routes.ts• L128-155 apps/…/routes.ts• L189-203
Change checklist
When these files or sections are changed, remember to consider these:
- Keep provider registration aligned with every service used before the application reaches readiness.
- Keep database open, restore, setup state, transaction hooks, and readiness in the same lifecycle order.
- Keep shared route registration aligned with the server's authentication, CSRF, transaction, upload, and response contracts.
- Keep the desktop network gate ahead of every route that must be local-only. Keep trusted renderer access separate from browser access.
- Keep WebSocket attachment aligned with messaging-provider capability and reverse-proxy upgrade support.
- Keep configuration precedence aligned with listener binding, HTTPS, proxy trust, data paths, and container deployment.
- Keep outbound request policy aligned with URL trust. Note content must not receive operator-endpoint access.
- Keep temporary upload ownership aligned with process lifetime, response close, size limits, and importer expectations.
- Keep resource definitions aligned with download progress, license state, file integrity, and data-directory placement.
- Keep bootstrap auth and view selection aligned with the per-request desktop marker rather than the process-wide desktop build flag.
- Keep export format support aligned with the shared export contract and the CSS resources available in development and production.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Startup reports ready before the document or required services are usable | Clients reach partial state and fail in less clear ways | Complete core setup, application binding, and provider registration before readiness |
| A desktop network gate is registered after a protected route | A local-only service can answer a network request | Register the gate before static, web, sharing, API, and MCP paths |
| A browser on a desktop listener is treated as the trusted renderer | The browser skips login or receives renderer-only state | Check the request marker rather than the process-wide desktop flag |
| A transaction commits without publishing its entity changes | Connected clients keep stale note state | Keep commit hooks connected to live messaging |
| A reverse proxy drops the live connection upgrade | The application loads but clients do not receive updates | Forward upgrade headers and configure the trusted proxy boundary |
| A note-linked URL reaches a private address | Note content can make the server contact an internal service | Validate resolved addresses and pin the checked connection |
| A resource passes download completion without integrity checks | A feature can load incomplete or corrupt files | Require declared size and SHA-256 checks before use |
| A large import remains buffered in the request | Memory use grows with archive size | Stream suitable archives from temporary files and clean them after the response |
| An unsupported export format reaches the provider factory | Export fails instead of producing a defined result | Reject formats outside the shared export contract |
Key files
| File | Why to read it |
|---|---|
| apps/…/main.ts | Shows the Node entry point and fatal startup handling. |
| apps/…/core_init.ts | Shows document opening, provider registration, setup state, and transaction hooks. |
| apps/…/www.ts | Shows listener creation, live messaging attachment, feature registration, and readiness. |
| apps/…/app.ts | Shows middleware order and application composition. |
| apps/…/routes.ts | Shows how server-only and shared routes form the client surface. |
| apps/…/index.ts | Shows bootstrap state for setup, login, desktop, mobile, and signed-in clients. |
| apps/…/route_api.ts | Shows request context, transactions, response handling, and upload boundaries. |
| apps/…/sql_provider.ts | Shows the Node-to-core database boundary. |
| apps/…/config.ts | Defines deployment setting resolution. |
| apps/…/data_dir.ts | Defines durable and temporary server paths. |
| apps/…/request.ts | Shows the controlled outbound request boundary. |
| apps/…/safe_fetch.ts | Shows address, redirect, and response limits for note-linked requests. |
| apps/…/ws_messaging_provider.ts | Shows live client access and transport capability. |
| apps/…/resources.ts | Shows managed runtime resource state and integrity checks. |
| apps/…/factory.ts | Shows how the server supplies export providers and production or development content styles. |
| apps/…/share_provider.ts | Shows the read-only boundary for published content. |
| packages/…/index.ts | Defines the shared provider set supplied by a host. |
| packages/…/types.ts | Defines database capabilities expected by the shared core. |
| packages/…/types.ts | Defines the live messaging contract. |