Configuration file
Environment variables are stored in a.env file in the api/ directory. This file is excluded from version control for security reasons.
Edit the configuration
Open
.env in your text editor and set the appropriate values for your environment.Available variables
The following environment variables are available in the Lens Music API:Server configuration
Specifies the environment the application is running in.Accepted values:
development- Development mode with debug loggingproduction- Production mode with optimized settingstest- Testing environment
development.env
The port number on which the API server listens for incoming requests.Default:
8080.env
Make sure this port is available and not blocked by your firewall. The client application expects the API to be running on this port.
Database configuration
The API uses PostgreSQL for data persistence. All database variables are required for the application to function.The hostname or IP address of your PostgreSQL server.Common values:
localhost- For local development127.0.0.1- Alternative to localhostdb- Common in Docker Compose setups- Remote host address for production
.env
The port number on which PostgreSQL is listening.Default PostgreSQL port:
5432.env
The PostgreSQL username for database authentication.
.env
The password for the PostgreSQL user.
.env
Use a strong password for production environments. Consider using environment-specific secrets management services.
The name of the PostgreSQL database to connect to.The database must exist before starting the API server. Create it using:
.env
Authentication configuration
Secret key used to sign and verify JWT authentication tokens.
.env
Generate a secure secret using:
openssl rand -base64 32 or node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"Example configurations
Here are example configurations for different environments:Database connection
The API uses TypeORM to manage database connections. The connection is configured inapi/src/data-source.ts using the environment variables.
Connection features
Automatic synchronization
Automatic synchronization
In development mode (
NODE_ENV=development), TypeORM automatically synchronizes the database schema with your entity definitions. This means:- Tables are created automatically
- Columns are added when entities change
- Relationships are established
Connection pooling
Connection pooling
TypeORM uses connection pooling to efficiently manage database connections. Multiple API requests can reuse existing connections, improving performance.
Entity discovery
Entity discovery
The API automatically discovers and registers all entity files in
api/src/entities/.Troubleshooting
Connection refused error
Connection refused error
If you see “Connection refused” errors:
-
Verify PostgreSQL is running:
- Check that the host and port are correct
- Ensure PostgreSQL is configured to accept connections from your host
- Verify firewall rules allow connections to the PostgreSQL port
Authentication failed error
Authentication failed error
If you see “Authentication failed” errors:
- Verify your username and password are correct
-
Check that the user has permission to access the database:
-
Verify PostgreSQL’s authentication method in
pg_hba.conf
Database does not exist error
Database does not exist error
If you see “Database does not exist” errors:
-
Create the database:
-
Verify the
DB_NAMEvariable matches your database name
Port already in use error
Port already in use error
If the API port is already in use:
-
Check what’s using the port:
-
Either stop the conflicting process or change the
PORTvariable to an available port
Security best practices
Never commit .env files
Keep
.env files out of version control. The .gitignore file already excludes them.Use strong passwords
Generate strong, unique passwords for database users, especially in production.
Restrict database access
Create dedicated database users with minimal required permissions.
Rotate credentials regularly
Update passwords and credentials periodically, especially after team member changes.
Related resources
Development setup
Complete guide to setting up your environment
Database schema
Learn about the database structure
Monorepo structure
Understand the codebase organization