A robust Node.js/TypeScript backend API for managing feature flags, user targeting, and A/B experiments with comprehensive metrics and analytics.
- Feature Flag Management: Create, update, and manage feature flags
- User Targeting: Enable/disable features for specific users
- Experiment Management: Create and manage A/B tests with variant distribution
- Metrics & Analytics: Track flag usage, events, and dashboard metrics
- RESTful API: Clean, well-documented REST endpoints
- API Documentation: Interactive Swagger/OpenAPI documentation
- Type Safety: Full TypeScript support with Zod validation
- PostgreSQL: Robust relational database with proper schema design
- Runtime: Node.js
- Framework: Express.js
- Language: TypeScript
- Database: PostgreSQL
- Validation: Zod
- API Documentation: Swagger/OpenAPI (swagger-jsdoc, swagger-ui-express)
- Architecture: Service-oriented with separation of concerns
- Node.js (v18 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
-
Clone the repository (if not already done)
cd backend -
Install dependencies
npm install
-
Set up environment variables
cp .env.example .env
Edit
.envwith your configuration:PORT=3001 NODE_ENV=development # PostgreSQL Configuration DB_HOST=localhost DB_PORT=5432 DB_NAME=skitt DB_USER=postgres DB_PASSWORD=postgres
-
Create PostgreSQL database
# Connect to PostgreSQL psql -U postgres # Create database CREATE DATABASE skitt; # Exit psql \q
-
Start the server
npm run dev
The server will automatically:
- Initialize the database schema
- Start on the configured port (default: 3001)
- Set up Swagger documentation at
http://localhost:3001/api-docs
backend/
βββ src/
β βββ config/ # Configuration files (Swagger, etc.)
β βββ database/ # Database connection and initialization
β βββ middleware/ # Express middleware (error handling, async)
β βββ models/ # Database models/data access layer
β βββ routes/ # API route definitions (endpoints only)
β βββ schemas/ # Zod validation schemas
β βββ services/ # Business logic layer
β βββ index.ts # Application entry point
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore rules
βββ package.json # Dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ README.md # This file
The backend follows a clean architecture pattern with clear separation of concerns:
- Routes: Handle HTTP requests/responses only, delegate to services
- Services: Contain all business logic and validation
- Models: Data access layer (database queries)
- Schemas: Input validation using Zod
- Middleware: Error handling and async wrapper
Request β Route β Service β Model β Database
β
Validation (Zod)
β
Error Handling
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/flags |
Get all feature flags |
| GET | /api/flags/:id |
Get feature flag by ID |
| POST | /api/flags |
Create a new feature flag |
| PUT | /api/flags/:id |
Update a feature flag |
| DELETE | /api/flags/:id |
Delete a feature flag |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/users |
Get all users |
| GET | /api/users/:id |
Get user by ID |
| POST | /api/users |
Create a new user |
| PUT | /api/users/:id |
Update a user |
| DELETE | /api/users/:id |
Delete a user |
| GET | /api/users/:userId/flags |
Get flags assigned to a user |
| POST | /api/users/:userId/flags/:flagId |
Assign flag to user |
| DELETE | /api/users/:userId/flags/:flagId |
Remove flag from user |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/experiments |
Get all experiments |
| GET | /api/experiments/:id |
Get experiment by ID |
| GET | /api/experiments/flag/:flagId |
Get experiments by flag ID |
| POST | /api/experiments |
Create a new experiment |
| PUT | /api/experiments/:id |
Update an experiment |
| DELETE | /api/experiments/:id |
Delete an experiment |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/metrics/events |
Track a flag event |
| GET | /api/metrics |
Get metrics for all flags |
| GET | /api/metrics/flags/:flagId |
Get metrics for a specific flag |
| GET | /api/metrics/dashboard |
Get dashboard summary |
| Method | Endpoint | Description |
|---|---|---|
| GET | /health |
Health check endpoint |
Interactive API documentation is available via Swagger UI:
URL: http://localhost:3001/api-docs
The Swagger UI provides:
- Complete API endpoint documentation
- Request/response schemas
- Try-it-out functionality
- Example requests and responses
# Development mode with hot reload
npm run dev
# Build TypeScript to JavaScript
npm run build
# Run production build
npm start
# Type checking without building
npm run type-check- Use TypeScript for all code
- Follow the existing architecture pattern
- Keep routes thin (endpoints only)
- Put all logic in services
- Use Zod schemas for validation
- Handle errors using AppError class
- Create/Update Schema in
src/schemas/ - Add Service Method in
src/services/ - Add Route in
src/routes/ - Add Swagger Documentation in route file
- Update Swagger Config if needed in
src/config/swagger.ts
- feature_flags: Core feature flag definitions
- users: User information and attributes
- user_flag_assignments: User-specific flag overrides
- experiments: A/B test configurations
- experiment_assignments: User experiment assignments
- flag_events: Event tracking for analytics
The database schema is automatically initialized on server start.
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 3001 |
NODE_ENV |
Environment (development/production) | development |
DB_HOST |
PostgreSQL host | localhost |
DB_PORT |
PostgreSQL port | 5432 |
DB_NAME |
Database name | db_name |
DB_USER |
Database user | user |
DB_PASSWORD |
Database password | password |
curl -X POST http://localhost:3001/api/flags \
-H "Content-Type: application/json" \
-d '{
"key": "new-checkout-flow",
"name": "New Checkout Flow",
"description": "Enable the new checkout experience",
"enabled": false
}'curl -X POST http://localhost:3001/api/users/user123/flags/1 \
-H "Content-Type: application/json" \
-d '{
"enabled": true
}'curl -X POST http://localhost:3001/api/metrics/events \
-H "Content-Type: application/json" \
-d '{
"flag_id": 1,
"user_id": "user123",
"event_type": "viewed",
"metadata": {"page": "checkout"}
}'The API uses consistent error responses:
{
"error": "Error message"
}Error status codes:
400: Bad Request (validation errors)404: Not Found409: Conflict (duplicate resources)500: Internal Server Error
ISC
- Follow the existing code structure
- Keep routes thin - all logic in services
- Add Zod schemas for validation
- Update Swagger documentation
- Test your changes
- Push your codes
For issues and questions, please refer to the main project documentation.