Prerequisites
Before deploying the backend, ensure you have:- Node.js 20.x or higher
- MongoDB instance (local or cloud-hosted like MongoDB Atlas)
- Redis instance (optional but recommended for session management)
- npm or pnpm package manager
Installation
Install dependencies
Install all required packages:
Key Dependencies
The backend uses these major dependencies:- express - Web framework
- mongoose - MongoDB ODM
- redis - Redis client for caching and sessions
- passport - Authentication middleware
- passport-google-oauth20 - Google OAuth authentication
- jsonwebtoken - JWT token generation and validation
- bcrypt - Password hashing
- helmet - Security headers
- cors - Cross-origin resource sharing
- express-rate-limit - Rate limiting middleware
- zod - Schema validation
Configure environment variables
Create a See the Environment Variables page for complete configuration details.
.env file in the apps/server directory:Set up MongoDB
Ensure your MongoDB instance is running and accessible. The server expects:
- A MongoDB connection URI in the
MONGO_URIenvironment variable - The database will be created automatically on first connection
- Collections for:
users,licenses,clients, andprojects
MongoDB Models
MongoDB Models
KeyBox uses four primary MongoDB collections:
- User - User accounts and authentication
- License - License keys and metadata
- Client - Client applications using licenses
- Project - Projects grouping licenses
Build and Run
Development Mode
Run the server with hot-reloading:ts-node-dev to automatically restart the server when files change.
Production Build
Build TypeScript
Compile TypeScript to JavaScript:This creates compiled files in the
dist/ directory.Deployment Options
Docker Deployment
Create aDockerfile in the apps/server directory:
Cloud Platform Deployment
Deploy to Vercel
Deploy to Vercel
The backend is configured for Vercel serverless deployment:
- Install Vercel CLI:
npm i -g vercel - Run
vercelin theapps/serverdirectory - Configure environment variables in Vercel dashboard
- Deploy:
vercel --prod
Ensure your MongoDB and Redis instances are accessible from Vercel’s network.
Deploy to Railway/Render
Deploy to Railway/Render
- Connect your GitHub repository
- Set the root directory to
apps/server - Set build command:
npm run build - Set start command:
node dist/server.js - Configure environment variables
- Deploy
Deploy to AWS/DigitalOcean
Deploy to AWS/DigitalOcean
- Set up a VPS or EC2 instance
- Install Node.js, MongoDB (or use managed service)
- Clone repository and install dependencies
- Configure environment variables
- Use PM2 or systemd for process management
- Set up nginx as reverse proxy
- Configure SSL with Let’s Encrypt
Database Setup
MongoDB Indexes
For optimal performance, create indexes on frequently queried fields:Mongoose will create some indexes automatically based on schema definitions.
Security Considerations
Configure CORS
Updatesrc/app.ts to restrict origins:
Health Checks
The server exposes a health check endpoint:Monitoring and Logs
Logging
The server uses Morgan for HTTP request logging in development mode. For production:- Use a logging service like Winston or Pino
- Send logs to external service (Datadog, LogRocket, etc.)
- Monitor error rates and response times
Process Management
Use PM2 for production process management:Troubleshooting
MongoDB Connection Issues
Redis Connection Issues
Port Already in Use
Next Steps
- Frontend Setup - Deploy the Next.js dashboard
- Environment Variables - Complete environment configuration
- Authentication Setup - Configure Google OAuth