Production-ready starter template for building APIs with Express 5 and TypeScript. It includes authentication with JWT access/refresh tokens, Prisma ORM, Zod validation, Swagger docs, security middleware, logging, and opinionated tooling.
- Express 5 + TypeScript with strict compiler options
- Authentication: JWT access tokens + HTTP-only cookie refresh tokens
- Password hashing: argon2
- Database: Prisma ORM (PostgreSQL)
- Validation: Zod schemas and a reusable validation middleware
- API Docs: Swagger (OpenAPI) via swagger-jsdoc and swagger-ui-express
- Security: Helmet, rate limiting, compression, CORS
- Error handling: Centralized error middleware and graceful shutdown
- Logging: Winston console logger
- Tooling: ESLint + Prettier, scripts for dev/build/start
backend/
src/
config/ # prisma client, swagger setup
controllers/ # route handlers
middleware/ # auth, validation, error, refresh-token
routes/ # route definitions
types/ # request/response types (e.g., AuthRequest)
utils/ # hash, jwt, logger
validators/ # zod schemas
index.ts # app entry
prisma/
schema.prisma
migrations/
docs/
API.md # expanded API guide
- Node.js 18+
- PostgreSQL database
This starter works with any PostgreSQL database. If you use Supabase as your Postgres host:
- Set
DATABASE_URLinbackend/.envto your Supabase connection string - You can manage schema in one of two ways:
Schema management options:
- Prisma manages migrations (applied to Supabase)
cd backend
# Edit prisma/schema.prisma
npx prisma migrate dev --name <change-name>
npx prisma generateUse this if you want this repo to be the source of truth for DB schema. Commit the prisma/migrations/ folder.
- Supabase manages schema
cd backend
npx prisma db pull # pull schema from Supabase into schema.prisma
npx prisma generate # generate client; safe, no DB changesgit clone https://github.com/rafayhanan/Backend-Boilerplate.git
cd backend
npm installCreate an .env file in backend/ (example values shown):
DATABASE_URL=postgresql://user:password@localhost:5432/yourdb
DIRECT_URL=postgresql://user:password@localhost:5432/yourdb
ACCESS_TOKEN_SECRET=change-me
REFRESH_TOKEN_SECRET=change-me
PORT=5000Initialize the database (generate client and apply migrations):
npx prisma migrate dev --name initRun in development:
npm run dev
# Swagger UI: http://localhost:3000/api-docsBuild and run in production:
npm run build
npm startnpm run dev– Run with ts-node-dev (watch mode)npm run build– TypeScript compile todist/npm start– Run compiled app fromdist/npm run lint– Lint with ESLintnpm run format– Format with Prettier
- Refresh tokens are stored on the
Usermodel and set as an HTTP-only cookie namedrefreshToken. - If your frontend runs on a different origin, configure CORS and cookie attributes (
sameSite,secure) accordingly.
- High-level usage in this README
- Detailed endpoints: see
docs/API.mdor Swagger at/api-docswhen the server is running
Contributions are welcome!
- Fork the repo and create a feature branch:
git checkout -b feat/your-feature - Install dependencies and run locally:
npm installthennpm run dev - Ensure code is formatted and linted:
npm run format && npm run lint - Include concise descriptions in PRs.