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 install

2. 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

  1. Go to supabase.com
  2. Create new project
  3. 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:

  1. Enable 2-Factor Authentication
  2. Go to App Passwords
  3. Generate password for "Mail"
  4. Use in EMAIL_PASS

5. Start Server

# Development
pnpm dev

# Production
pnpm build
pnpm start

Server runs at: http://localhost:3001

๐Ÿงช Testing

# Unit tests
pnpm test:unit

# Integration tests
pnpm test:integration

# All tests
pnpm test

๐Ÿ” Security

โš ๏ธ Important:

  • Never commit .env to git
  • Never expose SUPABASE_SERVICE_ROLE_KEY to frontend
  • Use strong ADMIN_TOKEN_SECRET
  • Rotate keys regularly

On this page