Settings Configuration
The src/settings.ts file is the central configuration hub for your StratusTS application. This guide explains every configuration option in simple, easy-to-understand language.
Overview
Section titled “Overview”Your settings file controls:
- Which port your server runs on
- Which apps are loaded
- How to connect to databases
- Where templates are stored
Here’s what a typical settings file looks like:
import type { DatabaseType, SettingsType } from 'stratus-ts';
const PORT = 2000;const APPS = ['users', 'blog'];const DATABASE: DatabaseType = false;const TEMPLATE_DIR = 'templates';
export default { PORT, APPS, DATABASE, TEMPLATE_DIR,} satisfies SettingsType;Let’s break down each setting in detail.
PORT - Server Port
Section titled “PORT - Server Port”What It Does
Section titled “What It Does”The PORT setting determines which port number your server listens on.
const PORT = 2000;Understanding Ports
Section titled “Understanding Ports”Think of ports as doors to your server:
- Port
80: Standard HTTP (web browsers) - Port
443: Standard HTTPS (secure web) - Port
3000: Common for Node.js development - Port
2000: StratusTS default
Your server URL becomes: http://localhost:PORT
Common Values
Section titled “Common Values”const PORT = 2000; // Default - good for developmentconst PORT = 3000; // Popular alternativeconst PORT = 8080; // Common for APIsconst PORT = 4000; // Any available port worksUsing Environment Variables
Section titled “Using Environment Variables”For production, use environment variables:
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 2000;Why? Production servers (Heroku, Railway, etc.) assign ports dynamically.
Example: Dynamic Port Configuration
Section titled “Example: Dynamic Port Configuration”// Development: Use 2000// Production: Use whatever the hosting platform providesconst PORT = process.env.ENV === 'prod' ? parseInt(process.env.PORT || '8080') : 2000;Troubleshooting
Section titled “Troubleshooting”“Port already in use” error?
Change to a different port:
const PORT = 3000; // Try different numbers until one worksOr find and stop the process using that port:
# On macOS/Linuxlsof -ti:2000 | xargs kill -9
# On Windowsnetstat -ano | findstr :2000taskkill /PID <process_id> /FAPPS - Installed Applications
Section titled “APPS - Installed Applications”What It Does
Section titled “What It Does”The APPS array lists all the apps your application should load.
const APPS = ['users', 'blog', 'api'];How It Works
Section titled “How It Works”When your server starts:
- StratusTS reads this array
- Finds each app directory in
src/ - Imports
routes.tsfrom each app - Registers all routes automatically
No manual route configuration needed!
Empty Apps (Default)
Section titled “Empty Apps (Default)”const APPS = []; // No apps loadedUse this when you first create a project. Add apps as you build them.
Single App
Section titled “Single App”const APPS = ['users']; // Only users app is loadedMultiple Apps
Section titled “Multiple Apps”const APPS = [ 'users', 'blog', 'api', 'admin',];Order matters! Apps are loaded in this order. If two apps define the same route, the first one wins.
Example: Adding a New App
Section titled “Example: Adding a New App”- Create the app:
st create app payments- Add to settings:
const APPS = [ 'users', 'blog', 'payments', // New app added ];- Start your development server — routes are now active!
Best Practices
Section titled “Best Practices”Keep it organized:
const APPS = [ // Core functionality 'users', 'auth',
// Features 'blog', 'shop',
// Admin 'admin',];Start small, grow as needed:
// Small projectconst APPS = ['api'];
// As it growsconst APPS = ['users', 'blog', 'api'];
// Larger projectconst APPS = ['users', 'blog', 'api', 'admin', 'notifications'];TEMPLATE_DIR - Template Directory
Section titled “TEMPLATE_DIR - Template Directory”What It Does
Section titled “What It Does”Specifies where your EJS template files are stored.
const TEMPLATE_DIR = 'templates';Understanding the Path
Section titled “Understanding the Path”The path is relative to your project root:
const TEMPLATE_DIR = 'templates';
// Results in:my-project/├── templates/ ← Templates go here│ ├── home.ejs│ └── about.ejs├── src/└── package.jsonDefault Configuration
Section titled “Default Configuration”const TEMPLATE_DIR = 'templates';This is the default and works for most projects.
Custom Locations
Section titled “Custom Locations”You can change where templates are stored:
const TEMPLATE_DIR = 'views'; // Common alternative
my-project/├── views/ ← Templates here now└── src/const TEMPLATE_DIR = 'src/templates'; // Inside src
my-project/└── src/ ├── templates/ ← Templates here └── settings.tsNested Structure Example
Section titled “Nested Structure Example”const TEMPLATE_DIR = 'templates';
// Organize templates in subdirectories:templates/├── layouts/│ └── base.ejs├── partials/│ ├── header.ejs│ └── footer.ejs└── pages/ ├── home.ejs ├── about.ejs └── contact.ejsWhen to Change It
Section titled “When to Change It”Keep default ('templates') unless:
- Your team prefers
views(common in Express) - You want templates inside
srcdirectory - You have a specific directory structure requirement
Using Templates
Section titled “Using Templates”Once configured, render templates in controllers:
const homeController: ControllerType = (question, reply) => { reply.render(reply, 'pages/home', { title: 'Home Page', user: { name: 'Alice' }, });};Note: Path is relative to TEMPLATE_DIR:
TEMPLATE_DIR = 'templates'- Template at:
templates/pages/home.ejs - Use:
'pages/home'
If You Don’t Use Templates
Section titled “If You Don’t Use Templates”If you’re building a pure API without HTML pages:
const TEMPLATE_DIR = 'templates'; // Keep it, even if unusedIt won’t hurt to leave it—just don’t create template files.
Complete Configuration Examples
Section titled “Complete Configuration Examples”Example 1: Simple API (No Database, No Templates)
Section titled “Example 1: Simple API (No Database, No Templates)”import type { DatabaseType, SettingsType } from 'stratus-ts';
const PORT = 3000;const APPS = ['api'];const DATABASE: DatabaseType = false;const TEMPLATE_DIR = 'templates';
export default { PORT, APPS, DATABASE, TEMPLATE_DIR,} satisfies SettingsType;Example 2: Full-Stack App with PostgreSQL
Section titled “Example 2: Full-Stack App with PostgreSQL”Upcomming
Section titled “Upcomming”Example 3: Development vs Production
Section titled “Example 3: Development vs Production”Upcomming
Section titled “Upcomming”Best Practices
Section titled “Best Practices”1. Use Environment Variables for Secrets
Section titled “1. Use Environment Variables for Secrets”❌ Don’t do this:
password: 'my_secret_password', // NEVER commit passwords!✅ Do this:
password: process.env.DB_PASSWORD || '',2. Set Defaults for Development
Section titled “2. Set Defaults for Development”Upcomming
Section titled “Upcomming”3. Keep Settings Simple
Section titled “3. Keep Settings Simple”Don’t overcomplicate your settings file:
// Good - clear and simpleconst PORT = 2000;
// Overkill - too complexconst PORT = parseInt( process.env.PORT || process.env.APP_PORT || process.env.SERVER_PORT || '2000');4. Document Custom Configuration
Section titled “4. Document Custom Configuration”Add comments for non-obvious settings:
// Using custom port because 2000 conflicts with another serviceconst PORT = 3500;
// Only loading public-facing apps, admin is separate deploymentconst APPS = ['users', 'blog', 'api'];5. Validate Configuration
Section titled “5. Validate Configuration”Upcomming
Section titled “Upcomming”Testing Your Configuration
Section titled “Testing Your Configuration”Check Current Settings
Section titled “Check Current Settings”Add a debug script to see your configuration:
const SETTINGS = { PORT, APPS, DATABASE, TEMPLATE_DIR,} satisfies SettingsType;
// In development, log settingsif (process.env.NODE_ENV !== 'production') { console.log('Current settings:', SETTINGS);}
export default SETTINGS;Verify Database Connection
Section titled “Verify Database Connection”Test your database configuration:
pnpm dev # or npm run devLook for:
database connected successfullyserver running on port : 2000If you see connection errors, review your database settings.
Next Steps
Section titled “Next Steps”Now that you understand configuration:
- Creating Apps - Master app architecture