Configuration
Configure your AzuraJS application
Configuration βοΈ
AzuraJS uses a file-based configuration system that supports TypeScript, JSON, and YAML formats.
Configuration File π
Create an azura.config.ts file in your project root:
import type { ConfigTypes } from "azurajs/config";
const config: ConfigTypes = {
environment: "development",
server: {
port: 3000,
cluster: false,
ipHost: true,
https: false,
},
logging: {
enabled: true,
showDetails: true,
},
plugins: {
cors: {
enabled: true,
origins: ["*"],
methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
allowedHeaders: ["Content-Type", "Authorization"],
},
rateLimit: {
enabled: false,
limit: 100,
timeframe: 60000,
},
},
};
export default config;Configuration Options π§
Environment
environment: "development" | "production" | "test"Sets the application environment. Affects logging behavior and other environment-specific features.
In production mode, detailed logging is automatically disabled for better performance.
Server Configuration
port
port: numberThe port number your server will listen on. Default: 3000
server: {
port: 8080
}cluster
cluster: booleanEnable cluster mode to utilize all CPU cores. Default: false
server: {
cluster: true // Spawns a worker for each CPU core
}Cluster mode is recommended for production but not necessary for development.
ipHost
ipHost: booleanDisplay local network IP address on startup. Default: false
server: {
ipHost: true // Shows: http://192.168.1.100:3000
}https
https: booleanIndicates if the server is running behind HTTPS. Affects the req.protocol property. Default: false
Logging Configuration π
enabled
enabled: booleanEnable/disable request logging. Default: true
showDetails
showDetails: booleanShow detailed request/response information including:
- Request method and path
- Response status code
- Response time
- Request body (in development)
logging: {
enabled: true,
showDetails: true
}Example output:
β POST /api/users 201 Created (15ms)CORS Plugin π
Configure Cross-Origin Resource Sharing for your API.
enabled
enabled: booleanEnable/disable CORS plugin. Default: false
origins
origins: string | string[]Allowed origins for CORS requests.
cors: {
enabled: true,
origins: ["https://example.com", "https://app.example.com"]
// or allow all: origins: ["*"]
}methods
methods: string | string[]Allowed HTTP methods.
cors: {
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
}allowedHeaders
allowedHeaders: string | string[]Allowed request headers.
cors: {
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"]
}Rate Limiting Plugin π‘οΈ
Protect your API from abuse with built-in rate limiting.
enabled
enabled: booleanEnable/disable rate limiting. Default: false
limit
limit: numberMaximum number of requests per timeframe.
rateLimit: {
enabled: true,
limit: 100 // 100 requests per timeframe
}timeframe
timeframe: numberTimeframe in milliseconds.
rateLimit: {
limit: 100,
timeframe: 60000 // 100 requests per minute
}When rate limit is exceeded, the server responds with 429 Too Many Requests
Environment-Specific Configuration π
Use environment variables for different deployment environments:
import type { ConfigTypes } from "azurajs/config";
const isDev = process.env.NODE_ENV !== "production";
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const config: ConfigTypes = {
environment: isDev ? "development" : "production",
server: {
port,
cluster: !isDev, // Enable cluster only in production
ipHost: isDev, // Show IP only in development
https: !isDev,
},
logging: {
enabled: true,
showDetails: isDev, // Detailed logs only in development
},
plugins: {
cors: {
enabled: true,
origins: isDev
? ["*"]
: ["https://yourdomain.com"],
},
rateLimit: {
enabled: !isDev, // Rate limit only in production
limit: 100,
timeframe: 60000,
},
},
};
export default config;JSON Configuration π
You can also use JSON format:
{
"environment": "development",
"server": {
"port": 3000,
"cluster": false
},
"logging": {
"enabled": true,
"showDetails": true
}
}YAML Configuration π
Or YAML format:
environment: development
server:
port: 3000
cluster: false
logging:
enabled: true
showDetails: trueAccessing Configuration at Runtime π
You can access the configuration in your application:
const app = new AzuraClient();
const config = app.getConfig();
console.log(`Server port: ${config.server?.port}`);
console.log(`Environment: ${config.environment}`);Full Configuration Example π
Here's a complete production-ready configuration:
import type { ConfigTypes } from "azurajs/config";
const config: ConfigTypes = {
environment: "production",
server: {
port: 8080,
cluster: true,
ipHost: false,
https: true,
},
logging: {
enabled: true,
showDetails: false,
},
plugins: {
cors: {
enabled: true,
origins: [
"https://yourdomain.com",
"https://app.yourdomain.com"
],
methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
allowedHeaders: [
"Content-Type",
"Authorization",
"X-API-Key"
],
},
rateLimit: {
enabled: true,
limit: 1000,
timeframe: 60000, // 1000 requests per minute
},
},
};
export default config;