Javascript Framework for API and Admin Panel
npm install @ecopex/ecopex-frameworkA Node.js system built with Fastify, Knex, and Ajv for automatic routing and database management with PM2 process management.
- Fastify: High-performance web framework
- Knex: SQL query builder with migrations and seeds
- Ajv: JSON schema validation
- MySQL: Database support
- PM2: Process management for production
- Microservices: Separate admin and API services
- Automatic Routing: Dynamic route loading from directory structure
```
sb_system/
├── workers/
│ ├── admin.js # Admin service worker
│ └── api.js # API service worker
├── routes/
│ ├── admin/
│ │ └── users/
│ │ ├── route.js # Route definitions
│ │ ├── handler.js # Route handlers
│ │ └── validation.js # Validation schemas
│ └── api/
├── database/
│ ├── migrations/
│ └── seeds/
├── config/
│ └── database.js
├── utils/
│ ├── routeLoader.js
│ └── validator.js
├── logs/ # PM2 log files
├── ecosystem.config.js # PM2 configuration
└── package.json
1. Install dependencies:
`bash`
npm install
2. Set up environment variables:
`bash`Copy the example and configure your settings
cp env.example .env
3. Configure your environment in .env:`bashDatabase Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=sb_system
4. Run database migrations:
`bash
npm run migrate
`5. Seed the database (optional):
`bash
npm run seed
`> Note: See ENVIRONMENT.md for complete environment variable documentation.
Usage
$3
`bash
Start both services with PM2
npm run devOr start individual services for development
npm run dev:api # API service only
npm run dev:admin # Admin service only
`$3
`bash
Start all services
npm startOr start with production environment
npm run prod
`$3
`bash
View status
npm run statusView logs
npm run logsRestart services
npm run restartStop services
npm run stopReload services (zero-downtime)
npm run reloadDelete all services
npm run delete
`Services
$3
- Health Check: GET http://localhost:3000/health
- Documentation: GET http://localhost:3000/docs
- Welcome: GET http://localhost:3000/
- Language Management:
- GET /api/language - Get supported languages
- POST /api/language/set - Set preferred language
- GET /api/language/detect - Detect language from headers$3
- Health Check: GET http://localhost:3001/health
- Documentation: GET http://localhost:3001/docs
- User Management:
- GET /admin/users - Get all users (with pagination)
- GET /admin/users/:id - Get user by ID
- POST /admin/users - Create new user
- PUT /admin/users/:id - Update user
- DELETE /admin/users/:id - Delete userAdding New Routes
1. Create a new directory under
routes/admin/ or routes/api/
2. Add three files:
- route.js - Define your routes
- handler.js - Implement route handlers
- validation.js - Define validation schemas$3
`javascript
// routes/admin/products/route.js
module.exports = [
{
method: 'GET',
url: '',
handlerName: 'getAllProducts',
validationName: 'getAllProductsValidation',
schema: {
// Fastify schema definition
}
}
];// routes/admin/products/handler.js
class ProductHandler {
static async getAllProducts(request, reply) {
// Handler implementation
}
}
module.exports = ProductHandler;
// routes/admin/products/validation.js
module.exports = {
getAllProductsValidation: {
// Ajv validation schema
}
};
`Database
The system uses Knex.js for database operations with MySQL. Migrations and seeds are included for easy setup.
$3
- npm run migrate - Run migrations
- npm run migrate:rollback - Rollback migrations
- npm run seed - Run seedsMulti-Language Support
The system includes comprehensive internationalization (i18n) support with automatic language detection and validation error translation.
$3
- English (en) - Default
- Turkish (tr) - Türkçe
- Spanish (es) - Español$3
The system automatically detects language from:
1. Accept-Language header
2. X-Language header
3. lang query parameter
4. Falls back to default locale$3
`bash
Get supported languages
GET /api/languageSet preferred language
POST /api/language/set
{
"language": "tr"
}Detect language from headers
GET /api/language/detect
`$3
`bash
Request with Turkish language
curl -H "Accept-Language: tr" http://localhost:3000/api/languageRequest with custom language header
curl -H "X-Language: es" http://localhost:3000/admin/usersRequest with query parameter
curl "http://localhost:3000/admin/users?lang=tr"
`Validation
The system uses Ajv for JSON schema validation with multi-language error messages. Validation schemas are defined in the
validation.js` files and automatically applied to routes.MIT