Overview
MediGuide requires specific environment variables to run properly. This guide covers setting up your.env file and securing sensitive configuration data.
Required Environment Variables
Add database credentials
Configure your PostgreSQL database connection:
.env
These credentials are used in
db.js:8-13 to create the PostgreSQL connection pool.Complete .env Template
Here’s a complete.env template with all required variables:
.env
How Variables Are Used
Database Connection
The database credentials are loaded indb.js to create a PostgreSQL connection pool:
db.js
JWT Authentication
TheJWT_SECRET is used to sign and verify authentication tokens in the authentication middleware:
src/middleware/auth.js
src/routes/users.js
Server Port
ThePORT variable determines which port the Express server listens on:
server.js
Environment Mode
TheNODE_ENV variable controls environment-specific behavior. For example, in development mode, password reset codes are logged to the console:
src/routes/users.js
Security Best Practices
Gitignore Configuration
Add the following to your.gitignore:
.gitignore
Generating a Secure JWT Secret
Use one of these methods to generate a cryptographically secure JWT secret:Environment-Specific Configuration
Maintain separate configuration files for different environments:.env.development- Local development.env.staging- Staging environment.env.production- Production environment
Use tools like
dotenv-vault or secret management services (AWS Secrets Manager, HashiCorp Vault) for production environments.Database Password Security
Best Practices
- Use passwords with at least 16 characters
- Include uppercase, lowercase, numbers, and special characters
- Never use default passwords like
postgresoradmin - Rotate passwords regularly (every 90 days)
- Use different passwords for development and production
Connection String Alternative
You can also use a database connection string instead of individual variables:.env
db.js to use the connection string:
db.js
Verifying Configuration
After setting up your environment variables, verify the configuration works:Troubleshooting
Common Issues
Error: “Cannot connect to database” Check that:- PostgreSQL is running
- Database credentials are correct
- Database exists (create it with
createdb mediguide) - Firewall allows connections to DB_PORT
.envfile exists in the root directorydotenv.config()is called before using environment variables- JWT_SECRET is set in
.env
.env or kill the process using the port:
Next Steps
Database Setup
Learn how to set up and initialize the PostgreSQL database