ENV FILE SETUP
NestJS Backend API for GoGrab Space platform - Architecture, Best Practices, and Common Issues
🔧 Backend API - Environment Setup (REQUIRED!)
Date: November 18, 2025
Issue: Backend was failing with MongoDB errors because .env file was missing!
✅ FIXED - .env File Created
I've created a .env file at apis/backend-api/.env with the following configuration:
# MongoDB Connection (REQUIRED)
MONGODB_CONNECTION_STRING=mongodb+srv://username:[email protected]/?retryWrites=true&w=majority
MONGODB_DATABASE=gograb-dev
# JWT Secret (REQUIRED)
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
# Server Port
PORT=3000
# Node Environment
NODE_ENV=development
# CORS Origins
ALLOWED_ORIGINS=http://localhost:5200,http://localhost:5701,http://localhost:4200,http://localhost:5300
🚨 IMPORTANT - You MUST Update These Values:
1. MongoDB Connection String (REQUIRED):
Get your MongoDB Atlas connection string:
- Go to MongoDB Atlas
- Click "Connect" on your cluster
- Select "Connect your application"
- Copy the connection string
- Replace
usernameandpasswordwith your actual credentials
Example:
MONGODB_CONNECTION_STRING=mongodb+srv://paco:[email protected]/?retryWrites=true&w=majority
OR use local MongoDB:
MONGODB_CONNECTION_STRING=mongodb://localhost:27017/gograb-dev
2. JWT Secret (REQUIRED):
Generate a secure random string:
# Generate a secure JWT secret
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
Then update the .env file:
JWT_SECRET=a8f5f167f44f4964e6c998dee827110c03b8c2e9d3a37f78b4a6f7e8c2d9e1f0
🔄 After Updating .env:
Restart the backend:
# Press Ctrl+C in the terminal running the backend yarn serve:backend-apiCheck the logs - You should see:
🚀 ═══════════════════════════════════════════════════════ GoGrab Backend API - READY ═══════════════════════════════════════════════════════ 📡 Server: http://localhost:3000/api 🗄️ Database: gograb-dev 🌍 Environment: development 🔒 CORS Configuration: Total Origins: 4 1. http://localhost:5200 2. http://localhost:5701 3. http://localhost:4200 4. http://localhost:5300Test it - Try saving something in the frontend
🐛 Why This Was Causing Errors:
- No
.envfile → No MongoDB connection string - Backend couldn't connect to MongoDB → All write operations failed
- Frontend showed 500 errors → "Failed to save universe data"
Now that we have a .env file, the backend can connect to MongoDB! ✅
📚 Environment Variables Explained:
| Variable | Purpose | Required? |
|---|---|---|
MONGODB_CONNECTION_STRING |
MongoDB Atlas or local connection | ✅ YES |
MONGODB_DATABASE |
Database name | ✅ YES |
JWT_SECRET |
Secret for JWT token signing | ✅ YES |
PORT |
Backend server port | No (defaults to 3000) |
NODE_ENV |
Environment mode | No (defaults to development) |
ALLOWED_ORIGINS |
CORS allowed origins | No (has defaults) |
OPENAI_API_KEY |
OpenAI API access | No (only if using AI) |
ANTHROPIC_API_KEY |
Anthropic API access | No (only if using AI) |
🔒 Security Notes:
- ✅
.envis gitignored - Your secrets won't be committed - ⚠️ Never commit real credentials to Git
- ⚠️ Use different secrets for production
- ✅ Railway has its own environment variables - This is just for local
🎯 Quick Setup Checklist:
- Create
.envfile (DONE!) - Update
MONGODB_CONNECTION_STRINGwith your MongoDB Atlas connection - Update
JWT_SECRETwith a secure random string - Restart backend:
yarn serve:backend-api - Verify "MongoDB connected successfully" in logs
- Test saving in frontend
Status: ✅ .env file created - Now update the MongoDB connection string!