Overview
The Construction Backend API is optimized for deployment on Vercel, a serverless platform that provides automatic scaling and global CDN distribution. This guide covers the complete deployment process and configuration.Prerequisites
Before deploying, ensure you have:- A Vercel account (vercel.com)
- Git repository with your project code
- MongoDB Atlas cluster configured (see Database Setup)
- Vercel CLI installed (optional but recommended)
Vercel Configuration
The project includes avercel.json configuration file that defines the deployment settings.
Configuration Breakdown
vercel.json
Configuration Properties
| Property | Value | Description |
|---|---|---|
version | 2 | Vercel Build API version |
builds.src | src/index.js | Entry point for the application |
builds.use | @vercel/node | Node.js runtime builder |
routes.src | /(.*) | Catch-all route pattern |
routes.dest | src/index.js | Route all requests to main file |
The
@vercel/node builder automatically detects Node.js version from package.json engines field or uses the latest LTS version.Deployment Steps
Prepare Your Repository
Ensure your code is pushed to a Git repository (GitHub, GitLab, or Bitbucket).
Verify
.env is in your .gitignore to prevent committing sensitive data.Connect to Vercel
Using Vercel Dashboard:
- Log in to vercel.com
- Click “Add New Project”
- Import your Git repository
- Select the repository containing your API
- Click “Import”
Using Vercel CLI:
Configure Environment Variables
In the Vercel dashboard:
- Navigate to your project
- Go to “Settings” → “Environment Variables”
- Add the following variables:
| Name | Value | Environment |
|---|---|---|
MONGO_URI | Your MongoDB connection string | Production, Preview, Development |
PORT | Auto-set by Vercel | Leave empty |
Verify Deployment
After deployment completes:
- Vercel provides a deployment URL (e.g.,
https://your-project.vercel.app) - Test the health check endpoint:
- Expected response:
Check the Vercel deployment logs if you encounter issues. Logs are available in the Vercel dashboard under the deployment details.
Application Structure for Vercel
The API is structured as a serverless function. The main application file exports the Express app:src/index.js
The
export default app statement is crucial. Vercel uses this export to handle incoming requests.Environment-Specific Configuration
Development
For local development, use:nodemon for auto-reloading.
Production
Vercel automatically runs the production build. Ensure yourpackage.json includes:
package.json
Custom Domain Setup
Add Domain in Vercel
- Go to project “Settings” → “Domains”
- Click “Add Domain”
- Enter your domain name
- Click “Add”
Configure DNS
Vercel provides DNS records to configure:For apex domain (example.com):For subdomain (api.example.com):
Monitoring and Logs
View Deployment Logs
- Go to your project in Vercel dashboard
- Click on the deployment
- View “Build Logs” and “Function Logs”
Real-time Logs via CLI
Performance Optimization
Cold Start Mitigation
Serverless functions experience “cold starts” when idle. To minimize impact:- Keep functions warm: Use a monitoring service to ping your API periodically
- Optimize dependencies: Remove unused packages to reduce bundle size
- Use connection pooling: Mongoose handles this automatically
Response Caching
For cacheable endpoints, add cache headers:Troubleshooting
Build Failures
Problem: Build fails during deployment Solutions:- Check build logs in Vercel dashboard
- Verify all dependencies are in
package.json(not just devDependencies) - Ensure Node.js version compatibility
- Test build locally:
npm install && npm start
Function Timeout
Problem: “Function Execution Timeout” error Solutions:- Optimize database queries
- Check MongoDB connection stability
- Verify network access to MongoDB Atlas from Vercel (use 0.0.0.0/0)
- Consider upgrading Vercel plan for longer timeout limits
CORS Errors
Problem: CORS errors when accessing from frontend Solutions:- Verify
corsmiddleware is enabled (it is by default insrc/index.js:18) - Configure specific origins if needed:
Environment Variables Not Loading
Problem: Environment variables are undefined Solutions:- Verify variables are set in Vercel dashboard
- Check variable names match exactly (case-sensitive)
- Redeploy after adding new environment variables
- Ensure
dotenv.config()is called in development but not needed in production (Vercel injects variables directly)
Database Connection Fails
Problem: Can’t connect to MongoDB from Vercel Solutions:- Verify MongoDB Atlas Network Access allows 0.0.0.0/0
- Check
MONGO_URIenvironment variable is set correctly - Ensure connection string includes all required parameters
- Review function logs for specific error messages
Rollback Deployments
If a deployment causes issues:- Go to project in Vercel dashboard
- Click “Deployments”
- Find the last working deployment
- Click ”⋯” → “Promote to Production”
Security Best Practices
Use Environment Variables
Never hardcode secrets in
vercel.json or source code. Always use Vercel’s environment variable dashboard.Additional Resources
For questions or issues, contact Vercel support or check the Vercel community forums.