Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Celaya55/app-cr/llms.txt

Use this file to discover all available pages before exploring further.

Environment Variables

App CR uses environment variables to configure database connections, authentication, and server settings. This guide covers all required and optional environment variables.

Required Variables

These environment variables must be set for the application to run:

DATABASE_URL

The PostgreSQL database connection string used by Prisma.
DATABASE_URL="postgresql://user_admin:password123@localhost:5432/mi_db_crud"
Format: postgresql://[user]:[password]@[host]:[port]/[database]
When using Docker Compose, replace localhost with the service name (e.g., postgres) if your backend runs in a container.

JWT_SECRET

Secret key used to sign and verify JWT tokens for authentication.
JWT_SECRET="your-secret-key-here"
Never commit your JWT_SECRET to version control. Use a strong, randomly generated string for production environments.

PORT

The port on which the Express.js server will run. Defaults to 3000 if not specified.
PORT=3000

Configuration Setup

1

Create .env File

Create a .env file in the backend directory:
cd backend
touch .env
2

Add Environment Variables

Add the following variables to your .env file:
.env
# Database Configuration
DATABASE_URL="postgresql://user_admin:password123@localhost:5432/mi_db_crud"

# Authentication
JWT_SECRET="your-super-secret-jwt-key-change-this-in-production"

# Server Configuration
PORT=3000
3

Add .env to .gitignore

Ensure your .env file is not tracked by Git:
.gitignore
# Environment variables
.env
.env.local
.env.*.local
4

Create .env.example

Create a template file for other developers:
.env.example
DATABASE_URL="postgresql://user:password@localhost:5432/database"
JWT_SECRET="your-secret-key"
PORT=3000

Environment-Specific Configuration

Development Environment

.env.development
# Development Database
DATABASE_URL="postgresql://user_admin:password123@localhost:5432/mi_db_crud"

# Development JWT Secret (less secure is okay)
JWT_SECRET="dev-secret-key"

# Development Port
PORT=3000

Docker Environment

When running the backend in Docker alongside the PostgreSQL container:
.env.docker
# Use service name instead of localhost
DATABASE_URL="postgresql://user_admin:password123@postgres:5432/mi_db_crud"

JWT_SECRET="your-secret-key"
PORT=3000

Production Environment

For production deployments:
.env.production
# Production Database (use managed database)
DATABASE_URL="postgresql://prod_user:strong_password@db.example.com:5432/prod_db"

# Strong JWT Secret (use random generator)
JWT_SECRET="<generated-256-bit-secret>"

# Production Port
PORT=3000
In production, use environment variables from your hosting platform (Railway, Heroku, AWS, etc.) instead of .env files.

Variable Usage in Code

The application loads environment variables using the dotenv package:
backend/index.js
require('dotenv').config();

const PORT = process.env.PORT || 3000;
const TOKEN = process.env.JWT_SECRET;

app.listen(PORT, () => {
  console.log(`Servidor corriendo en: http://localhost:${PORT}`);
});
Prisma automatically reads DATABASE_URL from the environment:
schema.prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Generating Secure Values

JWT Secret

Generate a secure random string for your JWT_SECRET:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

Database Connection String Components

Breakdown of the DATABASE_URL format:
ComponentDescriptionExample
ProtocolDatabase typepostgresql://
UserDatabase usernameuser_admin
PasswordDatabase passwordpassword123
HostDatabase server addresslocalhost or postgres
PortDatabase port5432
DatabaseDatabase namemi_db_crud

Validation and Testing

Check Environment Variables

Verify that your environment variables are loaded correctly:
console.log('PORT:', process.env.PORT);
console.log('JWT_SECRET:', process.env.JWT_SECRET ? '✓ Set' : '✗ Missing');
console.log('DATABASE_URL:', process.env.DATABASE_URL ? '✓ Set' : '✗ Missing');

Test Database Connection

Test your database connection with Prisma:
cd backend
npx prisma db pull
If successful, your DATABASE_URL is configured correctly.

Common Issues

Missing Environment Variables

If environment variables are missing, the application may fail to start or use default values that aren’t suitable for your environment.
Solution: Ensure your .env file exists in the backend directory and contains all required variables.

Connection Refused

If you see “connection refused” errors:
  1. Verify PostgreSQL is running: docker-compose ps
  2. Check the host in your DATABASE_URL (use localhost for local, postgres for Docker)
  3. Ensure the port matches your Docker configuration

Invalid JWT Secret

If authentication fails:
  1. Verify JWT_SECRET is set and matches across all instances
  2. Ensure the value doesn’t contain quotes in the .env file
  3. Restart your application after changing the secret

Environment Variable Checklist

Before deploying, verify:
  • .env file exists in the backend directory
  • DATABASE_URL is set and points to your database
  • JWT_SECRET is set with a strong random value
  • PORT is set (or defaults to 3000)
  • .env is listed in .gitignore
  • .env.example exists for team reference
  • Production values use strong passwords and secrets

Next Steps

Docker Deployment

Set up Docker Compose for your environment

Production Best Practices

Learn how to securely deploy to production