Overview
The DEMET Backend API uses environment variables for configuration management through thedotenv package. This approach keeps sensitive data out of your codebase and allows different configurations for development, staging, and production environments.
Environment Variables
All configuration is stored in a.env file at the root of your project.
Required Variables
PostgreSQL connection stringFormat:
postgresql://username:password@host:port/databaseExample: postgresql://postgres:mypassword@localhost:5432/demet_dbSecret key for signing access tokens (JWT)Recommendation: Generate a strong random string (32+ characters)Example:
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6Expiration time for access tokensFormat: Uses
jsonwebtoken time spans (e.g., 60, 2d, 10h, 7d)Default: 1h (1 hour)Recommendation: Short duration (15m - 1h) for better securitySecret key for signing refresh tokens (JWT)Important: Must be different from
ACCESS_SECRETExample: z9y8x7w6v5u4t3s2r1q0p9o8n7m6l5k4Expiration time for refresh tokensDefault:
7d (7 days)Recommendation: Longer duration (7d - 30d) for better UXPort number where the server will listenDefault:
3002Range: 1024-65535 (avoid reserved ports)Gmail address for sending emails via NodemailerExample:
[email protected] or [email protected]Gmail App Password (not your regular password)Setup: Generate at Google App PasswordsFormat: 16-character string without spaces
Administrator email address for receiving notificationsExample:
[email protected]Purpose: Receives alerts about new reservation requestsComplete Configuration Example
Configuration by Environment
Development Environment
Production Environment
Loading Configuration
The application loads environment variables at startup:Security Best Practices
1. Add .env to .gitignore
.gitignore
2. Use Different Secrets Per Environment
- Do This
- Don't Do This
3. Rotate Secrets Regularly
Change JWT secrets periodically:- Development: Monthly
- Production: Quarterly or after suspected breach
4. Use Strong Passwords
Generate secure random strings:5. Database Connection Security
- Production
- Development
Gmail Configuration
Setting Up App Passwords
Enable 2-Factor Authentication
Go to Google Account Security and enable 2FA
Generate App Password
Visit App Passwords
- Select app: “Mail”
- Select device: “Other (Custom name)”
- Enter: “DEMET Backend”
- Click “Generate”
Regular Gmail passwords won’t work. You must use an App Password.
JWT Token Configuration
Token Structure
The API uses dual-token authentication:- Access Token - Short-lived, stored in HTTP-only cookie
- Refresh Token - Long-lived, used to get new access tokens
Token Expiration Strategy
Access Token
Duration: 15m - 1hWhy short? Minimizes damage if compromisedStored: HTTP-only cookie
Refresh Token
Duration: 7d - 30dWhy long? Better user experienceStored: HTTP-only cookie
Example Configuration
CORS Configuration
CORS is configured inserver.js:
Validation
Verify your configuration:Troubleshooting
.env file not loading
.env file not loading
- Ensure
.envis in the project root - Check file name (no spaces, no extensions)
- Verify
dotenv.config()is called before using variables - Try absolute path:
dotenv.config({ path: '/path/to/.env' })
Database connection fails
Database connection fails
- Verify PostgreSQL is running
- Test connection string format
- Check network access and firewall rules
- Validate username/password
Email sending fails
Email sending fails
- Use App Password, not regular password
- Enable 2FA on Gmail account
- Check for spaces in GOOGLE_PWD
- Verify “Less secure app access” is OFF (use App Passwords instead)
JWT verification fails
JWT verification fails
- Ensure ACCESS_SECRET and REFRESH_SECRET are different
- Check for leading/trailing whitespace
- Verify secrets match on all servers
- Don’t change secrets without invalidating existing tokens
Next Steps
Database Setup
Configure PostgreSQL schema and connections
Email Notifications
Set up email templates and sending
Authentication
Learn about JWT authentication endpoints
Deployment
Deploy to production environments