A production-ready REST API boilerplate with Node.js, Express, TypeScript, and Prisma.
- TypeScript - Type-safe development
- Express.js - Fast, unopinionated web framework
- Prisma - Modern database toolkit
- PostgreSQL - Robust, open-source database
- JWT Authentication - Secure token-based authentication
- Role-based Access Control - Flexible permission system
- Input Validation - Request validation with Zod and zParse
- Error Handling - Centralized error handling
- Logging - Structured logging with Winston
- Rate Limiting - API rate limiting
- CORS - Cross-origin resource sharing
- Helmet - Security headers
- Swagger/OpenAPI - API documentation
- Docker - Containerization support
- PM2 - Process management
- Testing - Unit and integration tests with Vitest
- ESLint & Prettier - Code quality and formatting
- Husky - Git hooks
- Semantic Release - Automated versioning
- Node.js 20+
- PostgreSQL
- pnpm or npm
- Clone the repository:
git clone <repository-url>
cd express-prisma-typescript-boilerplate
- Install dependencies:
pnpm install
- Set up environment variables:
cp .env.example .env
- Update the
.env
file with your configuration:
NODE_ENV=development
PORT=8000
DATABASE_URL="postgresql://username:password@localhost:5432/database_name"
JWT_SECRET=your-jwt-secret
- Set up the database:
pnpm db:migrate
- Start the development server:
pnpm dev
The API will be available at http://localhost:8000
src/
├── config/ # Configuration files
│ ├── config.ts # Main configuration
│ ├── logger.ts # Logging configuration
│ ├── morgan.ts # HTTP request logging
│ ├── passport.ts # Authentication strategy
│ ├── roles.ts # Role-based access control
│ └── limiter.ts # Rate limiting
├── modules/ # Feature modules
│ ├── auth/ # Authentication module
│ ├── user/ # User management
│ ├── post/ # Example post module
│ ├── notification/ # Notifications
│ └── file/ # File uploads
├── shared/ # Shared utilities
│ ├── middlewares/ # Express middlewares
│ ├── services/ # Shared services
│ └── utils/ # Utility functions
├── routes/ # Route definitions
├── types/ # TypeScript type definitions
└── index.ts # Application entry point
This boilerplate uses Zod for schema-based validation and a utility called zParse
for parsing and validating Express requests. Validation schemas are defined using Zod in each module, and controllers use zParse
to validate and extract typed data from the request. This approach provides type safety, clear error messages, and a consistent validation experience.
Example:
// post.validation.ts
import { z } from 'zod';
export const createPostSchema = z.object({
body: z.object({
title: z.string().min(1, 'Title is required'),
content: z.string().min(1, 'Content is required'),
published: z.boolean().optional(),
}),
});
// post.controller.ts
import zParse from '@/shared/utils/z-parse';
import * as postSchema from './post.validation';
const createPost = catchAsync(async (req, res) => {
const { body } = await zParse(postSchema.createPostSchema, req);
// ...
});
Once the server is running, you can access the API documentation at:
- Swagger UI:
http://localhost:8000/v1/docs
- OpenAPI JSON:
http://localhost:8000/v1/docs-json
pnpm dev
- Start development serverpnpm build
- Build for productionpnpm start
- Start production serverpnpm test
- Run testspnpm test:watch
- Run tests in watch modepnpm coverage
- Generate test coveragepnpm lint
- Run ESLintpnpm lint:fix
- Fix ESLint errorspnpm prettier
- Check code formattingpnpm prettier:fix
- Fix code formattingpnpm db:studio
- Open Prisma Studiopnpm db:migrate
- Run database migrationspnpm docker:dev
- Start with Docker (development)pnpm docker:prod
- Start with Docker (production)
The boilerplate includes Docker configuration for easy development and deployment.
pnpm docker:dev
pnpm docker:prod
The main docker-compose.yml
includes PostgreSQL and Redis services:
docker compose up --build
This will start:
- Express API server
- PostgreSQL database
- Redis cache
Variable | Description | Default |
---|---|---|
NODE_ENV |
Environment | development |
PORT |
Server port | 8000 |
DATABASE_URL |
Database connection string | - |
JWT_SECRET |
JWT secret key | secret |
JWT_ACCESS_EXPIRATION_MINUTES |
JWT access token expiration | 30 |
JWT_REFRESH_EXPIRATION_DAYS |
JWT refresh token expiration | 30 |
SMTP_HOST |
SMTP server host | - |
SMTP_PORT |
SMTP server port | - |
SMTP_USERNAME |
SMTP username | - |
SMTP_PASSWORD |
SMTP password | - |
EMAIL_FROM |
Email sender address | - |
REDIS_URL |
Redis connection string | - |
SENTRY_DSN |
Sentry DSN for error tracking | - |
LOGTAIL_SOURCE_TOKEN |
Logtail source token | - |
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Run linting and tests
- Submit a pull request
MIT License - see LICENSE file for details.