Environment Setup
Development vs Production
Separate environment variables for different stages:.env.local
.env.production
Environment Validation
Validate required environment variables at startup:main.ts
Building for Production
Build TypeScript
Compile your TypeScript code:This generates JavaScript files in the
dist/ directory.Production Scripts
Update yourpackage.json:
package.json
Docker Deployment
Dockerfile
Create a production-ready Dockerfile:Dockerfile
Docker Compose
For local development and testing:docker-compose.yml
Build and Run
Docker Optimization
Multi-stage builds reduce image size:.dockerignore
AWS Lambda Deployment
Lambda Handler
Create a Lambda-compatible handler:lambda.ts
SAM Template
AWS Serverless Application Model (SAM) template:template.yaml
Deploy to Lambda
Vercel Deployment
Vercel Configuration
Create a serverless function:api/mcp.ts
Vercel Config
vercel.json
Deploy to Vercel
Railway Deployment
Railway provides simple container deployment:Set environment variables
Add environment variables in Railway dashboard:
AUTH0_DOMAINAUTH0_CLIENT_IDAUTH0_AUDIENCEPORT(Railway provides this)
Health Checks
Implement health checks for monitoring:main.ts
Monitoring and Logging
Structured Logging
Use structured logging for better observability:Error Tracking
Integrate error tracking:Performance Optimization
Enable Compression
Request Rate Limiting
Caching
Security Best Practices
Helmet.js
Add security headers:CORS Configuration
Secret Management
Use secret management services:- AWS: AWS Secrets Manager
- Vercel: Environment Variables
- Railway: Environment Variables
- Self-hosted: HashiCorp Vault
Troubleshooting
Container Won’t Start
Problem: Docker container exits immediately. Solutions:- Check logs:
docker logs <container-id> - Verify environment variables are set
- Test build locally:
node dist/main.js - Check for missing dependencies
Lambda Timeout
Problem: Lambda function times out. Solutions:- Increase timeout in SAM template
- Optimize cold start with provisioned concurrency
- Use stateless mode:
stateless: true - Reduce dependencies
Memory Issues
Problem: Out of memory errors. Solutions:- Increase memory limit in Dockerfile/Lambda
- Check for memory leaks
- Use streaming for large data
- Implement proper cleanup in finally blocks
Next Steps
Creating Services
Build production-ready services
Error Handling
Handle errors in production
Auth Integration
Secure your production deployment
Examples
See deployment examples