This example demonstrates how to use Type Config Core with vanilla Node.js (no framework).
- Profile-based configuration
- Hot reload support
- Type-safe configuration classes with decorators
- No framework dependencies (pure Node.js)
- Built-in HTTP server
- Graceful shutdown handling
- Environment variable substitution
# Install dependencies
yarn install
# Run in development mode
yarn devThe example uses YAML configuration files located in the config/ directory:
application.yml- Default configurationapplication-production.yml- Production profile configuration
Configuration is automatically merged based on the active profile.
GET /- Welcome message with app infoGET /config- Display bound configuration objectsGET /raw-config- Display raw configuration (all properties)GET /health- Health check endpoint
# Run with development profile (default)
yarn dev
# Run with production profile
NODE_ENV=production yarn dev
# Build and run compiled version
yarn build
NODE_ENV=production node dist/index.js@ConfigurationProperties('server')
class ServerConfig {
@ConfigProperty()
port: number = 3000;
@ConfigProperty()
host: string = 'localhost';
}@ConfigurationProperties('database')
class DatabaseConfig {
@ConfigProperty()
@Required()
host: string;
@ConfigProperty()
@DefaultValue(5432)
port: number;
@ConfigProperty()
username: string;
@ConfigProperty()
password: string;
}@ConfigurationProperties('app')
class AppConfig {
@ConfigProperty()
@DefaultValue('Node.js App')
name: string;
@ConfigProperty()
@DefaultValue('1.0.0')
version: string;
@ConfigProperty()
environment: string;
@ConfigProperty()
debug: boolean;
}This example showcases the core ConfigManager API:
// Create and initialize
const configManager = new ConfigManager({
profile: process.env.NODE_ENV || 'development',
configDir: './config',
enableHotReload: true,
});
await configManager.load();
// Register configuration classes
configManager.register(ServerConfig);
configManager.register(DatabaseConfig);
configManager.register(AppConfig);
// Bind instances
const serverConfig = configManager.bind(ServerConfig);
const dbConfig = configManager.bind(DatabaseConfig);
// Access properties
console.log(serverConfig.port); // 3000
console.log(dbConfig.host); // localhost
// Get all config
const allConfig = configManager.getAll();
// Get active profile
const profile = configManager.getProfile();
// Listen for changes
configManager.onChange((newConfig) => {
console.log('Config changed!', newConfig);
});When enableHotReload is true, the configuration is automatically reloaded when files in the config directory change. This is useful during development.
To test hot reload:
- Start the server with
yarn dev - Modify
config/application.yml - Watch the console for the reload message
- Visit
/configto see the updated values
You can use environment variable substitution in your config files:
database:
password: ${DB_PASSWORD}Then set the environment variable:
export DB_PASSWORD=my-secret-password
yarn devThe example includes graceful shutdown handling for SIGTERM and SIGINT signals, ensuring the server closes cleanly.