Overview
TailStack uses a clean separation between application configuration (app.ts) and server initialization (server.ts). This architecture makes it easy to test your application logic separately from server concerns.
Server Architecture
The server setup follows a two-file pattern:- app.ts - Configures the Express application with middleware and routes
- server.ts - Initializes the cluster and starts the HTTP server
Application Setup (app.ts)
Theapp.ts file configures your Express application with middleware and routing:
packages/core/source/Server/src/app.ts
Key Features
Middleware Stack
The application uses a carefully ordered middleware stack:
- CORS middleware for cross-origin requests
- JSON body parser for API requests
- URL-encoded body parser for form data
- Cookie parser for session management
API Routes
All API routes are mounted under the
/api prefix for clear separation between API and other endpoints.Server Initialization (server.ts)
Theserver.ts file handles cluster initialization and graceful shutdown:
packages/core/source/Server/src/server.ts
Graceful Shutdown
The server implements graceful shutdown to handle:- SIGTERM - Termination signal from process managers or orchestrators
- SIGINT - Interrupt signal from Ctrl+C in development
- The server stops accepting new connections
- Existing connections are allowed to complete
- The process exits cleanly with code 0
Graceful shutdown ensures that in-flight requests are not interrupted, providing a better experience for clients and preventing data loss.
Starting the Server
Start your development server:Environment Variables
The server reads configuration from environment variables:| Variable | Default | Description |
|---|---|---|
PORT | 5000 | Port number for the server |
NODE_ENV | development | Environment mode |
CORS_ORIGIN | http://localhost:5173 | Allowed CORS origin |
WORKERS | CPU count | Number of cluster workers |
Next Steps
Clustering
Learn how TailStack uses Node.js clustering for multi-core performance
Routing
Explore Express routing patterns and create API endpoints
Middleware
Configure CORS, security, and custom middleware
Configuration
Manage environment variables and server configuration