π Production-ready serverless API for Vercel deployment
β
Serverless Architecture - Optimized for Vercel deployment
β
User Registration with OTP verification
β
Email Service via Gmail SMTP
β
Secure Authentication with bcrypt & JWT
β
Contact Form with email notifications
β
Rate Limiting & security features
β
MongoDB Atlas cloud database
β
Auto-scaling & high availability
- Push code to GitHub
- Connect repository to Vercel
- Configure environment variables
- Deploy automatically
# 1. Install dependencies
npm install
# 2. Configure environment variables
# Create .env file with required variables
# 3. Start development server (Vercel Dev)
npm run dev
# Server will run on: http://localhost:3000Base URL: https://apis.forexking.info
GET /healthResponse:
{
"success": true,
"message": "ForexKing Backend API is running",
"timestamp": "2025-10-27T12:00:00.000Z",
"environment": "production"
}POST /api/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "Strong@Pass123",
"phone_number": "+201234567890"
}Response:
{
"success": true,
"message": "Registration successful. Please check your email for OTP verification code.",
"email": "john@example.com",
"name": "John Doe"
}POST /api/verify-otp
Content-Type: application/json
{
"email": "john@example.com",
"otp": "123456"
}Response:
{
"id": "507f1f7***********",
"name": "John Doe",
"email": "john@example.com",
"accessToken": "eyJhbGciOiJ*******..."
}POST /api/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "Strong@Pass123"
}Response:
{
"id": "507f1f77b********",
"name": "John Doe",
"email": "john@example.com",
"accessToken": "eyJhbGciOiJIUzI************..."
}POST /api/send-otp
Content-Type: application/json
{
"email": "john@example.com"
}Response:
{
"success": true,
"message": "OTP resent successfully. Please check your email."
}POST /api/contact
Content-Type: application/json
{
"name": "Jane Smith",
"email": "jane@example.com",
"message": "I would like to know more about your services."
}Response:
{
"success": true,
"message": "Message sent successfully. We will get back to you soon."
}- Minimum 8 characters
- At least one uppercase letter (A-Z)
- At least one lowercase letter (a-z)
- At least one number (0-9)
- At least one special character (!@#$%^&*)
- Valid email format
- Unique (not already registered)
- E.164 international format
- Must start with
+ - Unique (not already registered)
- 2-100 characters
- Required
Sent immediately after registration with 6-digit OTP code.
Expiry: 15 minutes
Sent after successful account verification.
Sent to company email when someone submits contact form.
Required environment variables must be configured in Vercel Dashboard:
| Variable | Description |
|---|---|
MONGODB_URI |
MongoDB Atlas connection string |
JWT_SECRET |
Secret for JWT signing |
JWT_EXPIRE |
Token expiry duration |
EMAIL_USER |
Gmail email address |
EMAIL_PASS |
Gmail App Password |
COMPANY_EMAIL |
Email to receive contact forms |
NODE_ENV |
Environment mode |
- π bcrypt password hashing (10 rounds)
- π« JWT token authentication
- π‘οΈ Security headers (CORS, Content-Type)
- π¦ Rate limiting per IP address
- Register: 10 req / 15 min
- Login: 10 req / 15 min
- Contact: 5 req / 15 min
- OTP Resend: 5 req / 15 min
- β Input validation with custom validators
- β° OTP expiration (15 minutes)
- π CORS enabled for all origins
backend/
βββ api/ # Serverless functions
β βββ _lib/ # Shared utilities
β β βββ serverless-helpers.js # DB, CORS, validation
β β βββ email-service.js # Email functionality
β βββ register.js # POST /api/register
β βββ login.js # POST /api/login
β βββ verify-otp.js # POST /api/verify-otp
β βββ send-otp.js # POST /api/send-otp (resend)
β βββ contact.js # POST /api/contact
β βββ health.js # GET /health
β βββ index.js # GET / (welcome)
βββ models/
β βββ User.js # User schema & methods
βββ utils/
β βββ generateToken.js # JWT utilities
β βββ generateOTP.js # OTP generation
βββ templates/
β βββ otpEmail.html # OTP email template
β βββ welcomeEmail.html # Welcome email template
β βββ contactNotification.html # Contact notification
βββ vercel.json # Vercel configuration
βββ package.json # Dependencies
βββ env.example # Environment variables template
βββ VERCEL_DEPLOYMENT.md # Deployment guide
βββ README.md # This file
Legacy Files (kept for backward compatibility):
server.js,routes/,controllers/,config/- Traditional Express setup
{
"express": "^4.18.2",
"mongoose": "^8.0.0",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.7",
"dotenv": "^16.3.1",
"cors": "^2.8.5",
"express-validator": "^7.0.1",
"helmet": "^7.1.0",
"express-rate-limit": "^7.1.5"
}For detailed deployment instructions and setup guides, contact the development team.
All errors return consistent JSON format:
{
"success": false,
"message": "Error description",
"errors": ["Detailed errors if validation"]
}Common HTTP Status Codes:
200- Success201- Created (registration)400- Bad Request (validation error)401- Unauthorized (invalid credentials)403- Forbidden (account not verified)404- Not Found429- Too Many Requests (rate limit)500- Server Error
Use tools like:
- Postman - API testing GUI
- cURL - Command line
- Thunder Client - VS Code extension
Example test flow:
- Register user β Receive OTP email
- Verify OTP β Get access token
- Login with credentials β Get access token
- Use token for protected routes
# Install dependencies
npm install
# Run with Vercel dev server (recommended)
npm run dev
# Deploy to preview environment
npm run deploy:preview
# Deploy to production
npm run deployManual Deployment:
npm run deployMustafagaberr Β© 2025