Backend API Setup
Complete guide to setting up and configuring the Express.js backend service.
๐ Prerequisites
- Node.js: 20+
- pnpm: 10.20.0+
- Supabase Account: For database and auth
๐ Quick Start
1. Clone and Install
cd Micro-Service/Backend
pnpm install2. Configure Environment
Create .env file:
# Server
PORT=3001
FRONTEND_URL=http://localhost:3000
# Supabase
SUPABASE_URL=https://xxx.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
# Authentication
ADMIN_TOKEN_SECRET=your-secret-key-here
# Email (Nodemailer)
EMAIL_PROVIDER=nodemailer
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_SECURE=false
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password
EMAIL_FROM="Portfolio Feedback" <noreply@yourdomain.com>
EMAIL_TO=recipient@example.com
# Email (Resend - Alternative)
# EMAIL_PROVIDER=resend
# RESEND_API_KEY=your-resend-api-key
# RESEND_FROM_EMAIL="Portfolio" <noreply@yourdomain.com>3. Supabase Setup
Create Project
- Go to supabase.com
- Create new project
- Get credentials from Settings โ API
Run Migrations
Execute in Supabase SQL Editor:
1. Sync Auth Users (migrations/sync_auth_users.sql):
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS trigger AS $$
BEGIN
INSERT INTO public.users (id, email, name, created_at)
VALUES (NEW.id, NEW.email, NEW.raw_user_meta_data->>'name', NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();2. Add Feedback Fields (migrations/add_feedback_fields.sql):
ALTER TABLE feedback ADD COLUMN IF NOT EXISTS location TEXT;
ALTER TABLE feedback ADD COLUMN IF NOT EXISTS category TEXT;
ALTER TABLE feedback ADD COLUMN IF NOT EXISTS screenshot TEXT;4. Gmail App Password
For Nodemailer with Gmail:
- Enable 2-Factor Authentication
- Go to App Passwords
- Generate password for "Mail"
- Use in
EMAIL_PASS
5. Start Server
# Development
pnpm dev
# Production
pnpm build
pnpm startServer runs at: http://localhost:3001
๐งช Testing
# Unit tests
pnpm test:unit
# Integration tests
pnpm test:integration
# All tests
pnpm test๐ Security
โ ๏ธ Important:
- Never commit
.envto git - Never expose
SUPABASE_SERVICE_ROLE_KEYto frontend - Use strong
ADMIN_TOKEN_SECRET - Rotate keys regularly