A comprehensive NestJS backend API with authentication, Prisma ORM, PostgreSQL database, and Swagger documentation.
- Node.js 22 - Latest LTS version
- NestJS Framework - Progressive Node.js framework
- Prisma ORM - Next-generation ORM for Node.js and TypeScript
- PostgreSQL Database - Robust relational database
- JWT Authentication - Secure token-based authentication
- Swagger Documentation - Interactive API documentation
- Unit Testing - Comprehensive test coverage with Jest
- TypeScript - Full TypeScript support
- Validation - Request validation with class-validator
- CORS Support - Cross-origin resource sharing enabled
- Node.js 22 or higher
- PostgreSQL database
- npm or yarn package manager
-
Clone the repository
git clone <repository-url> cd nest-backend-api
-
Install dependencies
yarn install
-
Environment Setup
cp env.example .env
Update the
.envfile with your database credentials:DATABASE_URL="postgresql://username:password@localhost:5432/database_name" JWT_SECRET="your-super-secret-jwt-key" JWT_EXPIRES_IN="7d" PORT=3000
-
Database Setup
# Generate Prisma client yarn prisma:generate # Push schema to database yarn prisma:push # Or run migrations yarn prisma:migrate
-
Start the application
# Development mode yarn start:dev # Production mode yarn build yarn start:prod
nest-backend-api/
βββ src/
β βββ auth/ # Authentication module
β β βββ dto/ # Data Transfer Objects
β β β βββ register.dto.ts
β β β βββ login.dto.ts
β β β βββ auth-response.dto.ts
β β βββ guards/ # Authentication guards
β β β βββ jwt-auth.guard.ts
β β β βββ local-auth.guard.ts
β β βββ strategies/ # Passport strategies
β β β βββ jwt.strategy.ts
β β β βββ local.strategy.ts
β β βββ auth.controller.ts # Authentication endpoints
β β βββ auth.service.ts # Authentication business logic
β β βββ auth.module.ts # Authentication module
β βββ users/ # Users module
β β βββ users.controller.ts # User endpoints
β β βββ users.service.ts # User business logic
β β βββ users.module.ts # User module
β βββ prisma/ # Database module
β β βββ prisma.service.ts # Prisma client service
β β βββ prisma.module.ts # Prisma module
β βββ common/ # Shared utilities
β β βββ decorators/ # Custom decorators
β β βββ guards/ # Global guards
β β βββ interceptors/ # Global interceptors
β β βββ pipes/ # Global pipes
β β βββ utils/ # Utility functions
β βββ config/ # Configuration files
β βββ app.controller.ts # Main app controller
β βββ app.service.ts # Main app service
β βββ app.module.ts # Main app module
β βββ main.ts # Application entry point
βββ prisma/
β βββ schema.prisma # Database schema
βββ test/ # E2E tests
β βββ app.e2e-spec.ts
β βββ jest-e2e.json
βββ package.json
βββ tsconfig.json
βββ nest-cli.json
βββ README.md
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /auth/register |
Register a new user | No |
| POST | /auth/login |
Login user | No |
| GET | /auth/profile |
Get user profile | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /users/profile |
Get current user profile | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | / |
API health check | No |
| GET | /health |
Health status | No |
Once the application is running, you can access the Swagger documentation at:
- Swagger UI: http://localhost:3000/api
# Unit tests
yarn test
# E2E tests
yarn test:e2e
# Test coverage
yarn test:cov
# Watch mode
yarn test:watch- Unit Tests: Located in
*.spec.tsfiles alongside source code - E2E Tests: Located in the
test/directory - Coverage: Generated in the
coverage/directory
# Generate Prisma client
yarn prisma:generate
# Push schema changes to database
yarn prisma:push
# Create and run migrations
yarn prisma:migrate
# Open Prisma Studio (database GUI)
yarn prisma:studioThe application uses a simple User model:
model User {
id String @id @default(cuid())
email String @unique
password String
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}The API uses JWT (JSON Web Tokens) for authentication:
- Register: Create a new user account
- Login: Authenticate and receive a JWT token
- Protected Routes: Include the JWT token in the Authorization header
# Register
curl -X POST http://localhost:3000/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"password123","name":"John Doe"}'
# Login
curl -X POST http://localhost:3000/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"password123"}'
# Access protected route
curl -X GET http://localhost:3000/auth/profile \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Make sure to set the following environment variables in production:
DATABASE_URL="your-production-database-url"
JWT_SECRET="your-production-jwt-secret"
JWT_EXPIRES_IN="7d"
PORT=3000
NODE_ENV="production"yarn build
yarn start:prod| Script | Description |
|---|---|
yarn build |
Build the application |
yarn start |
Start the application |
yarn start:dev |
Start in development mode with hot reload |
yarn start:debug |
Start in debug mode |
yarn start:prod |
Start in production mode |
yarn test |
Run unit tests |
yarn test:watch |
Run tests in watch mode |
yarn test:cov |
Run tests with coverage |
yarn test:e2e |
Run e2e tests |
yarn lint |
Run ESLint |
yarn format |
Format code with Prettier |
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License.
If you have any questions or need help, please open an issue in the repository.
Happy Coding! π