This guide will help you deploy your Node.js/Express backend to Vercel with all features working.
- Vercel Account: Sign up at vercel.com
- Vercel CLI (Optional but recommended):
npm install -g vercel
- MongoDB Atlas Account: Your database should be hosted on MongoDB Atlas or another cloud MongoDB provider
- Vercel Blob Storage: Already configured in your project via
@vercel/blob
First, ensure your project builds successfully:
# Install dependencies
npm install
# Build the TypeScript project
npm run buildThis will compile your TypeScript code from src/ to dist/.
- β
vercel.json- Vercel configuration - β
.vercelignore- Files to exclude from deployment - β
Modified
src/server.ts- Now compatible with Vercel serverless
# Test locally after build
npm start-
Login to Vercel Dashboard: Go to vercel.com/dashboard
-
Import Project:
- Click "Add New" β "Project"
- Import your Git repository (GitHub, GitLab, or Bitbucket)
- Or upload your project folder directly
-
Configure Build Settings:
- Framework Preset: Other
- Root Directory:
./(leave as is) - Build Command:
npm run build - Output Directory:
dist - Install Command:
npm install
-
Configure Environment Variables (See Step 4 below)
-
Deploy: Click "Deploy"
-
Login to Vercel:
vercel login
-
Deploy:
# First deployment (will ask configuration questions) vercel # For production deployment vercel --prod
-
Follow the prompts:
- Set up and deploy? Y
- Which scope? Select your account
- Link to existing project? N (first time)
- Project name? l2h-blog-backend (or your choice)
- Directory? ./backend (if in monorepo) or ./
- Override settings? N
You MUST set up these environment variables in Vercel:
-
Database:
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/dbname?retryWrites=true&w=majority -
JWT Secrets:
JWT_SECRET=your-super-secret-jwt-key-min-32-characters JWT_REFRESH_SECRET=your-super-secret-refresh-key-min-32-characters -
Frontend URL (for CORS):
FRONTEND_URL=https://your-frontend.vercel.app,https://yourdomain.com -
Vercel Blob Storage (for file uploads):
BLOB_READ_WRITE_TOKEN=vercel_blob_rw_xxxxxxxxxxxx- Get this from: Vercel Dashboard β Your Project β Storage β Create Store β Blob
-
Email Service (for notifications):
EMAIL_SERVICE=gmail EMAIL_USER=your-email@gmail.com EMAIL_PASSWORD=your-app-specific-password EMAIL_FROM=L2H Blog <your-email@gmail.com> -
Rate Limiting (Optional):
RATE_LIMIT_WINDOW_MS=900000 RATE_LIMIT_MAX_REQUESTS=100
- Go to your project in Vercel Dashboard
- Click "Settings" β "Environment Variables"
- Add each variable with:
- Name: Variable name (e.g.,
MONGODB_URI) - Value: The actual value
- Environment: Select "Production", "Preview", and "Development" (all three)
- Name: Variable name (e.g.,
- Click "Save"
# Add environment variables one by one
vercel env add MONGODB_URI production
vercel env add JWT_SECRET production
vercel env add FRONTEND_URL production
# ... etc- Go to Vercel Dashboard β Your Project β "Storage"
- Click "Create Database" β Select "Blob"
- Name it (e.g., "l2h-uploads")
- Click "Create"
- Copy the
BLOB_READ_WRITE_TOKENand add it to your environment variables - The token will be automatically available in your Vercel deployment
After deployment, your backend will be at: https://your-project.vercel.app
Update your frontend to point to this URL:
// In your frontend config
const API_URL = 'https://your-backend.vercel.app/api';Update FRONTEND_URL environment variable in Vercel:
FRONTEND_URL=https://your-frontend.vercel.app,https://yourdomain.com
- Go to MongoDB Atlas Dashboard
- Navigate to "Network Access"
- Click "Add IP Address"
- Select "Allow Access from Anywhere" (0.0.0.0/0)
β οΈ This is required for Vercel since it uses dynamic IPs- Your database is still protected by authentication
Ensure your MONGODB_URI uses the SRV format and includes:
- Correct username and password
retryWrites=true&w=majorityparameters- No special characters in password (URL encode if needed)
- Enable 2-Step Verification on your Google Account
- Create App Password:
- Go to Google Account β Security β 2-Step Verification β App passwords
- Select "Mail" and "Other (Custom name)"
- Name it "L2H Blog Backend"
- Copy the 16-character password
- Add to Vercel:
EMAIL_SERVICE=gmail EMAIL_USER=your-email@gmail.com EMAIL_PASSWORD=your-16-char-app-password EMAIL_FROM=L2H Blog <your-email@gmail.com>
-
Check Deployment Status: In Vercel Dashboard, ensure deployment is "Ready"
-
Test Health Endpoint:
curl https://your-project.vercel.app/api/health
-
Test API:
# Get categories curl https://your-project.vercel.app/api/categories # Get blogs curl https://your-project.vercel.app/api/blogs
-
Check Logs:
- Vercel Dashboard β Your Project β "Logs"
- Watch for any errors
Solution: Make sure npm run build runs successfully locally first
Solution:
- Check MONGODB_URI is correct
- Verify MongoDB Atlas Network Access allows 0.0.0.0/0
- Ensure password doesn't contain special characters (URL encode if needed)
Solution:
- Add your frontend URL to FRONTEND_URL environment variable
- Format:
https://domain1.com,https://domain2.com(comma-separated, no spaces)
Solution:
- Ensure BLOB_READ_WRITE_TOKEN is set
- Create a Blob store in Vercel Dashboard β Storage
Solution:
- Check Vercel logs: Dashboard β Your Project β "Logs"
- Verify all required environment variables are set
- Look for missing dependencies
Solution:
- This is normal for serverless functions
- First request may take 2-5 seconds
- Subsequent requests will be faster
- Consider keeping the function warm with a health check ping
Solution:
- Vercel free tier has 10s timeout
- Pro tier has 60s timeout
- Optimize long-running operations
- Consider using Vercel Edge Functions for faster response
- Push to your main branch
- Vercel automatically rebuilds and deploys
# From your project directory
vercel --prod- Logs: Vercel Dashboard β Your Project β "Logs"
- Analytics: Vercel Dashboard β Your Project β "Analytics"
- Usage: Vercel Dashboard β Your Project β "Usage"
- Enable Edge Caching: Add cache headers to GET endpoints
- Optimize MongoDB Queries: Use indexes, limit results
- Compress Responses: Already enabled via
compressionmiddleware - Rate Limiting: Already configured (100 requests per 15 min)
- Keep Functions Warm: Ping health endpoint every 5 minutes
- Vercel Docs: vercel.com/docs
- Vercel Support: vercel.com/support
- MongoDB Atlas Docs: docs.atlas.mongodb.com
- Environment variables are set
- MongoDB Atlas Network Access allows Vercel IPs (0.0.0.0/0)
- Vercel Blob Storage is created and token is set
- Email service is configured and tested
- Frontend URL is added to CORS whitelist
- Frontend is updated with backend URL
- API health check returns 200 OK
- Test key endpoints (blogs, categories, auth)
- Check Vercel logs for errors
- Test file upload functionality
- Test email sending functionality
Your backend is now live on Vercel! Your API is available at:
https://your-project.vercel.app/api
- Health:
https://your-project.vercel.app/api/health - Blogs:
https://your-project.vercel.app/api/blogs - Categories:
https://your-project.vercel.app/api/categories - eBooks:
https://your-project.vercel.app/api/ebooks
Remember: Update your frontend to use the new backend URL! π