Backend API Local Development
Setting up and running backend-api locally with auto-start
Backend API Local Development
This guide explains how to set up and run the backend-api locally with automatic startup when serving gograb-space or space-admin applications.
Prerequisites
Before you begin, ensure you have:
- Node.js v24.7.0 or higher
- Yarn package manager
- MongoDB Atlas account (or local MongoDB instance)
- API keys for AI providers (OpenAI, Anthropic, Google)
Environment Setup
1. Create .env.local File
Create a .env.local file in the project root directory with the following variables:
# MongoDB Configuration
MONGODB_CONNECTION_STRING=mongodb+srv://username:[email protected]/
MONGODB_DATABASE=gograb-api
# AI Provider API Keys
OPENAI_API_KEY=your-openai-key-here
ANTHROPIC_API_KEY=your-anthropic-key-here
GOOGLE_API_KEY=your-google-key-here
# JWT Configuration
JWT_SECRET=your-secure-jwt-secret-here
JWT_EXPIRATION=7d
# Server Configuration
PORT=3000
NODE_ENV=development
# CORS Configuration
ALLOWED_ORIGINS=http://localhost:5200,http://localhost:5300
2. Generate JWT Secret
Generate a secure JWT secret using Node.js:
node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"
Copy the output and paste it as your JWT_SECRET value in .env.local.
3. MongoDB Configuration
Ensure your MongoDB connection string:
- Has proper URL encoding for special characters
- Points to your MongoDB Atlas cluster or local instance
- The database user has appropriate permissions
Auto-Start Configuration
The backend-api is configured to automatically start when you serve gograb-space or space-admin applications.
How It Works
Both apps/gograb-space/project.json and apps/space-admin/project.json have the backend-api configured as a dependency:
"serve": {
"executor": "@angular-devkit/build-angular:dev-server",
"dependsOn": ["backend-api:serve"],
...
}
This means:
- Running
nx serve gograb-spaceautomatically starts backend-api first - Running
nx serve space-adminautomatically starts backend-api first - The backend-api runs on port 3000 by default
Starting the Application
Option 1: Start gograb-space (includes backend-api)
nx serve gograb-space
You should see output indicating:
- Backend-api starting first
- Backend-api running on http://localhost:3000/api
- gograb-space starting on http://localhost:5200
Option 2: Start space-admin (includes backend-api)
nx serve space-admin
You should see output indicating:
- Backend-api starting first
- Backend-api running on http://localhost:3000/api
- space-admin starting on http://localhost:5300
Option 3: Start backend-api only
If you want to run just the backend-api:
nx serve backend-api
Testing Locally
1. Health Check Endpoint
Test the health endpoint to verify the backend is running:
curl http://localhost:3000/api/health
Expected response:
{
"status": "ok",
"timestamp": "2025-01-15T12:00:00.000Z",
"service": "backend-api",
"version": "1.0.0"
}
2. Root Endpoint
Test the root API endpoint:
curl http://localhost:3000/api
3. Authentication Endpoints
Register a new user:
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "Test123!",
"name": "Test User"
}'
Login:
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "Test123!"
}'
Save the returned accessToken for authenticated requests.
4. AI Endpoints (Requires Authentication)
Test OpenAI endpoint:
TOKEN="your-jwt-token-here"
curl -X POST http://localhost:3000/api/ai/openai/chat \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"messages": [
{"role": "user", "content": "Hello!"}
],
"model": "gpt-4o-mini",
"maxTokens": 50
}'
Demo Page
A demo page is available to test backend connectivity:
http://localhost:5200/backend-api-status
This page provides:
- Connection status indicator
- Health check results
- Environment information
- API endpoint testing
- Response time monitoring
Troubleshooting
Backend Not Starting
Issue: Backend-api doesn't start automatically
Solution:
- Check that
.env.localexists in the project root - Verify all required environment variables are set
- Check if port 3000 is already in use:
lsof -i :3000 - Clear NX cache and try again:
npx nx reset
MongoDB Connection Errors
Issue: Cannot connect to MongoDB
Solution:
- Verify MongoDB connection string is correct
- Ensure special characters in password are URL-encoded
- Check that your IP address is whitelisted in MongoDB Atlas
- Verify network connectivity
CORS Errors
Issue: CORS errors when accessing backend from frontend
Solution:
- Verify
ALLOWED_ORIGINSincludes your frontend URL - Ensure no trailing slashes in ALLOWED_ORIGINS
- Restart backend-api after changing CORS configuration
JWT Errors
Issue: JWT validation fails
Solution:
- Verify
JWT_SECRETis set in.env.local - Ensure the token hasn't expired
- Check that the token is being sent in the Authorization header
Port Already in Use
Issue: Port 3000 is already in use
Solution:
- Find and kill the process using port 3000:
lsof -i :3000 kill -9 <PID> - Or change the port in
.env.local:PORT=3001 - Update frontend environment files to match the new port
Development Tips
Hot Reload
The backend-api supports hot reload during development. Changes to backend files will automatically restart the server.
Logging
Monitor backend logs in the terminal where you ran the serve command. Logs include:
- Server startup information
- Incoming requests
- Database connections
- Error messages
Environment Variables
Changes to .env.local require restarting the backend-api:
- Stop the serve process (Ctrl+C)
- Start again with
nx serve gograb-spaceornx serve space-admin
Testing with Multiple Apps
You can only run one app at a time that depends on backend-api:
- Either gograb-space OR space-admin
- Both will try to start backend-api on port 3000
- Use separate terminals if you need both running
Next Steps
- Railway Deployment - Deploy to production
- API Reference - Complete API documentation
- Backend API Overview - Architecture and features