A professional RESTful API built with NestJS, TypeORM, and PostgreSQL for a blog application. This project demonstrates clean architecture, best practices, and enterprise-level code structure.
- Authentication & Authorization: JWT-based authentication with Passport
- User Management: Complete CRUD operations for users
- Post Management: Create, read, update, and delete blog posts
- Database: PostgreSQL with TypeORM
- Validation: Class-validator for DTO validation
- Error Handling: Global exception filters
- Logging: Request/response logging interceptors
- Security: Password hashing with bcrypt, CORS enabled
- Framework: NestJS
- Database: PostgreSQL
- ORM: TypeORM
- Authentication: JWT + Passport
- Validation: class-validator, class-transformer
- Password Hashing: bcrypt
src/
├── main.ts # Application entry point
├── app.module.ts # Root module
├── config/
│ └── database.config.ts # Database configuration
├── common/
│ ├── dto/
│ │ └── pagination.dto.ts
│ ├── guards/
│ ├── interceptors/
│ │ ├── logging.interceptor.ts
│ │ └── transform.interceptor.ts
│ └── filters/
│ └── http-exception.filter.ts
├── auth/
│ ├── auth.module.ts
│ ├── auth.controller.ts
│ ├── auth.service.ts
│ ├── dto/
│ │ ├── login.dto.ts
│ │ └── register.dto.ts
│ ├── guards/
│ │ └── jwt-auth.guard.ts
│ └── strategies/
│ └── jwt.strategy.ts
├── users/
│ ├── users.module.ts
│ ├── users.controller.ts
│ ├── users.service.ts
│ ├── user.entity.ts
│ └── dto/
│ ├── create-user.dto.ts
│ └── update-user.dto.ts
└── posts/
├── posts.module.ts
├── posts.controller.ts
├── posts.service.ts
├── post.entity.ts
└── dto/
├── create-post.dto.ts
└── update-post.dto.ts
- Clone the repository
cd blog-api- Install dependencies
npm install-
Configure environment variables
- Copy
.envand update with your database credentials - Update
JWT_SECRETwith a secure random string
- Copy
-
Setup PostgreSQL Database
# Create database
createdb blog_db
# Or using psql
psql -U postgres
CREATE DATABASE blog_db;# Development mode
npm run start:dev
# Production mode
npm run build
npm run start:prodThe API will be available at: http://localhost:3000/api/v1
POST /api/v1/auth/register- Register new userPOST /api/v1/auth/login- Login userGET /api/v1/auth/profile- Get current user profile (Protected)
GET /api/v1/users- Get all users (Protected)GET /api/v1/users/:id- Get user by ID (Protected)POST /api/v1/users- Create userPATCH /api/v1/users/:id- Update user (Protected)DELETE /api/v1/users/:id- Delete user (Protected)
GET /api/v1/posts- Get all postsGET /api/v1/posts/:id- Get post by IDGET /api/v1/posts/author/:authorId- Get posts by authorPOST /api/v1/posts- Create post (Protected)PATCH /api/v1/posts/:id- Update post (Protected, Owner only)DELETE /api/v1/posts/:id- Delete post (Protected, Owner only)
The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:
- Register or login to get an access token
- Include the token in the Authorization header:
Authorization: Bearer <your-token>
curl -X POST http://localhost:3000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"username": "johndoe",
"password": "password123"
}'curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "password123"
}'curl -X POST http://localhost:3000/api/v1/posts \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <your-token>" \
-d '{
"title": "My First Post",
"content": "This is the content of my first post",
"published": true
}'# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov| Variable | Description | Default |
|---|---|---|
PORT |
Application port | 3000 |
DB_HOST |
PostgreSQL host | localhost |
DB_PORT |
PostgreSQL port | 5432 |
DB_USERNAME |
Database username | postgres |
DB_PASSWORD |
Database password | postgres |
DB_DATABASE |
Database name | blog_db |
JWT_SECRET |
JWT secret key | your-secret-key |
JWT_EXPIRES_IN |
Token expiration | 7d |
- Clean Architecture: Separation of concerns with modules, controllers, and services
- Dependency Injection: NestJS built-in DI container
- DTOs: Data Transfer Objects with validation
- Guards: Route protection with JWT authentication
- Interceptors: Request/response transformation and logging
- Filters: Global exception handling
- TypeORM: Database abstraction with entities and repositories
This project is licensed under the MIT License.
Built with ❤️ using NestJS