Learn how to configure your CanxJS application using the configuration file and environment variables.
The main application configuration lives in src/config/app.ts.
1// src/config/app.ts2export default {3name: "CanxJS App",4env: process.env.NODE_ENV || "development",5port: Number(process.env.PORT) || 3000,6key: process.env.APP_KEY || "your-secret-key",7debug: process.env.DEBUG === "true",89// You can add custom config here10security: {11cors: true,12rateLimit: true,13}14};
Configure your database connection in src/config/database.ts.
1// src/config/database.ts2import type { DatabaseConfig } from 'canxjs';34const config: DatabaseConfig = {5driver: process.env.DB_CONNECTION as any || "mysql",6host: process.env.DB_HOST || "localhost",7port: Number(process.env.DB_PORT) || 3306,8database: process.env.DB_NAME || "canx_app",9username: process.env.DB_USER || "root",10password: process.env.DB_PASS || "",11logging: process.env.NODE_ENV !== "production",12pool: { min: 2, max: 10 },13};1415export default config;
Use environment variables to store sensitive configuration. Create a .env file in your project root.
1# .env2NODE_ENV=development3PORT=300045# Database6DB_HOST=localhost7DB_PORT=33068DB_NAME=canx_app9DB_USER=root10DB_PASS=secret1112# Security13JWT_SECRET=your-secret-key14SESSION_SECRET=your-session-secret1516# Cache17REDIS_URL=redis://localhost:6379
You can customize the directory structure of your application by creating a canx.config.ts configuration file. This allows you to adopt different architectural patterns like NestJS or Express style structures.
1// canx.config.ts2export default {3paths: {4controllers: 'src/http/controllers',5models: 'src/domain/models',6middlewares: 'src/http/middleware',7migrations: 'database/migrations',8services: 'src/domain/services',9}10};
Core application settings
| Option | Type | Default | Description |
|---|---|---|---|
| port | number | 3000 | Server port |
| host | string | localhost | Server hostname |
| env | string | development | Environment mode |
| debug | boolean | false | Enable debug mode |
Database connection settings
| Option | Type | Default | Description |
|---|---|---|---|
| driver | string | mysql | Database driver (mysql, postgres) |
| host | string | localhost | Database host |
| port | number | 3306 | Database port |
| database | string | - | Database name |
| username | string | - | Database username |
| password | string | - | Database password |
Security and authentication settings
| Option | Type | Default | Description |
|---|---|---|---|
| cors.enabled | boolean | true | Enable CORS |
| cors.origin | string | array | * | Allowed origins |
| rateLimit.enabled | boolean | true | Enable rate limiting |
| rateLimit.max | number | 100 | Max requests per window |
| rateLimit.window | number | 60000 | Window in ms |