Overview
The Adoptme API uses environment variables for configuration, loaded via thedotenv package. This approach keeps sensitive data secure and allows different configurations for development, staging, and production environments.
Configuration Loading
The application loads configuration fromsrc/config/config.js:
How It Works
- dotenv.config(): Loads variables from
.envfile intoprocess.env - Default values: Provides fallbacks if environment variables aren’t set
- Export: Makes configuration available throughout the application
Default values are only used in development. Production deployments should always explicitly set environment variables.
Environment Variables Reference
Core Configuration
| Variable | Description | Type | Default | Required |
|---|---|---|---|---|
PORT | Port number for the HTTP server | Number | 8080 | No |
MONGO_URL | MongoDB connection URL | String | mongodb://127.0.0.1:27017 | Yes (production) |
DB_NAME | MongoDB database name | String | adoptme | Yes (production) |
PORT Configuration
Description: The port on which the Express server listens for HTTP requests. Examples:- Dockerfile exposes port 3000
- Railway typically uses port 3000
- Development default is 8080
- Can be any available port (1024-65535 for non-root users)
MONGO_URL Configuration
Description: Full connection string to your MongoDB instance, including protocol, credentials, host, and port. Format:| Parameter | Description | Example |
|---|---|---|
authSource | Authentication database | authSource=admin |
retryWrites | Retry write operations | retryWrites=true |
w | Write concern | w=majority |
ssl | Use SSL/TLS | ssl=true |
replicaSet | Replica set name | replicaSet=myRS |
DB_NAME Configuration
Description: Name of the MongoDB database to use for storing pets, users, and adoption data. Examples:- Database is created automatically if it doesn’t exist (with proper MongoDB permissions)
- Use different database names for different environments
- Default is
adoptmein all environments
Environment-Specific Configuration
Development Environment
Create a.env file in the project root:
The
.env file is excluded from version control via .gitignore. Never commit sensitive credentials.Production Environment (Railway)
Set environment variables in the Railway dashboard:- Always use explicit environment variables (no defaults)
- Use MongoDB Atlas or managed MongoDB service
- Enable authentication on MongoDB
- Use strong, randomly generated passwords
- Enable SSL/TLS for MongoDB connections
Docker Environment
Pass environment variables when running the container:.env file:
When using Docker on the same host as MongoDB, use
host.docker.internal instead of localhost to access the host machine.Security Considerations
Protecting Sensitive Data
Best Practices
- Use Environment Variables: Never hardcode credentials in source code
-
Strong Passwords: Use complex, randomly generated passwords for MongoDB
- Rotate Credentials: Regularly update MongoDB passwords and connection strings
- Limit Access: Configure MongoDB to only accept connections from known IPs
- Enable Authentication: Always require authentication in production MongoDB
-
Use SSL/TLS: Encrypt connections to MongoDB
- Principle of Least Privilege: Create MongoDB users with minimal required permissions
MongoDB User Permissions
Create a dedicated user for the application:Configuration Validation
Required Variables Check
The application should validate required configuration on startup. Add tosrc/config/config.js:
Troubleshooting
Application Won’t Start
Problem: Application crashes immediately after starting Solutions:- Verify all required environment variables are set
- Check MongoDB connection string format
- Ensure MongoDB is running and accessible
- Review application logs for specific error messages
Cannot Connect to MongoDB
Problem: “Failed to connect to MongoDB” error Solutions:- Verify
MONGO_URLis correct and accessible - Check network connectivity to MongoDB host
- Ensure MongoDB authentication credentials are valid
- Check firewall rules and security groups
- Verify MongoDB is running:
mongosh "$MONGO_URL"
Wrong Database Used
Problem: Application connects but uses wrong database Solutions:- Verify
DB_NAMEenvironment variable is set - Check if database name is included in connection string
- Ensure no conflicting configuration in application code
Environment Variables Not Loading
Problem: Application uses default values instead of.env file
Solutions:
- Verify
.envfile is in project root directory - Check file permissions (should be readable)
- Ensure variable names match exactly (case-sensitive)
- Restart the application after changing
.env - In Docker, verify variables are passed with
-eflag
For additional configuration options and advanced setup, refer to the Express.js and MongoDB documentation.