Production-ready SaaS scaffolding with subdomain multi-tenancy. Backend (Express + TS + MongoDB) and Frontend (Next.js) in a monorepo.
- Node.js 18+
- MongoDB 6+
- Backend
- Create
backend/.env:
PORT=4000
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=mawjaa
JWT_SECRET=change-me
JWT_EXPIRES_IN=7d
MAIN_DOMAIN=localhost
CORS_ORIGIN=http://localhost:3000
- Install and run:
cd backend
npm install
npm run dev- Frontend
- Create
frontend/.env.local:
NEXT_PUBLIC_API_URL=http://localhost:4000
NEXT_PUBLIC_MAIN_DOMAIN=localhost
- Install and run:
cd frontend
npm install
npm run dev- App landing:
http://localhost:3000/ - Storefront:
http://localhost:3000/mystore(or configure DNS/hosts to usemystore.localhost) - API base:
http://localhost:4000
- Register:
POST /api/auth/registeror via UI/register - Create store
/create-store(enforces single store per user) - Dashboard
/dashboardto manage products - Storefront
/[subdomain]lists products scoped bystoreId
- Use a wildcard DNS record
*.myapp.comto your frontend and route via Next.js middleware. - Set
NEXT_PUBLIC_MAIN_DOMAIN=myapp.comandMAIN_DOMAIN=myapp.comin envs. - Place backend behind HTTPS reverse proxy; allow CORS from your frontend origin.
- Configure process manager (PM2) or containerize; set proper secrets.