Overview
Clínica Vitalis uses environment variables to configure both the backend API server and optionally the frontend application. All sensitive configuration should be stored in.env files that are not committed to version control.
Backend Configuration
Required Environment Variables
Create a.env file in the backend/ directory with the following variables:
.env
The
.env file should never be committed to version control. Add it to .gitignore to prevent accidental commits.Environment Variable Reference
PORT
The port number on which the Express server will listen for incoming requests.Usage in code: Example values:
backend/models/server.ts:343000(development)8080(production)5000(alternative)
KEY_SECRET
Secret key used for signing and verifying JWT tokens. This should be a long, random string that is kept secure.Usage in code:Security recommendations:
backend/helpers/generateJWT.ts:12- Signing tokensbackend/middlewares/validatorJWT.ts- Verifying tokens
- Use at least 32 characters
- Include uppercase, lowercase, numbers, and special characters
- Generate using a secure random generator
- Never share or commit this value
KEY_FOR_ADMIN
Secret key used to authorize admin user registration. When this key is provided in the Security recommendations:
admin-key header during registration, the user will be created with admin privileges.Usage in code: backend/controllers/auth.ts:22- Use a long, random string (at least 32 characters)
- Keep this key highly confidential
- Only share with authorized personnel who need to create admin accounts
- Different from KEY_SECRET (use separate values)
Database Configuration
The database configuration is hardcoded inbackend/database/config.ts and uses SQLite:
backend/data/hospital.sqlite
Currently, the database path is not configurable via environment variables. If you need to change it, modify
backend/database/config.ts directly.JWT Token Configuration
JWT tokens are configured with the following settings inbackend/helpers/generateJWT.ts:13-14:
- Expiration time: 4 hours
- Algorithm: HS256 (default)
- Payload:
{ id: userId }
Frontend Configuration
Optional Environment Variables
Create a.env file in the hospital-staff-manager/ directory if you need to configure the API endpoint:
.env
Vite requires all environment variables that should be exposed to the client to be prefixed with
VITE_. Variables without this prefix will not be available in the browser.Environment Variable Reference
VITE_API_URL
The base URL of the backend API server.Example values:
http://localhost:3000(local development)https://api.clinicavitalis.com(production)http://192.168.1.100:3000(local network testing)
Configuration by Environment
Development
Production
Testing
Security Best Practices
Use Strong Secrets
Generate strong, random values for
KEY_SECRET using cryptographically secure methods.Use Different Secrets Per Environment
Use different
KEY_SECRET values for development, staging, and production environments.Rotate Secrets Regularly
Change your
KEY_SECRET periodically, especially after:- Team member departures
- Security incidents
- Suspected compromises
Rotating the JWT secret will invalidate all existing tokens, requiring users to log in again.
Loading Environment Variables
The backend loads environment variables using thedotenv package in backend/app.ts:1,5:
process.env values.
Validation and Error Handling
The application does not currently validate that required environment variables are set. Missing variables will result in
undefined values and runtime errors.Recommended Validation
Add validation tobackend/app.ts to fail fast if required variables are missing:
Troubleshooting
Environment Variables Not Loading
Symptoms: Application behaves as if environment variables are not set. Solutions:- Verify
.envfile is in the correct directory - Check that
dotenv.config()is called before using variables - Ensure no syntax errors in
.envfile (no quotes needed for values) - Restart the development server after changing
.env
JWT Errors
Symptoms: “JsonWebTokenError” or authentication failures. Solutions:- Verify
KEY_SECRETis set in.env - Ensure the secret is the same in all instances
- Check that tokens haven’t expired (4-hour lifetime)
- Verify no extra whitespace in
KEY_SECRETvalue
CORS Errors
Symptoms: Frontend cannot connect to backend. Solutions:- Verify backend is running and accessible
- Check
VITE_API_URLmatches backend address - Ensure CORS is enabled in backend (configured in
backend/models/server.ts:85)
