Skip to content

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.

Notifications You must be signed in to change notification settings

isamch/Blog-API---NestJS-Project

Repository files navigation

Blog API - NestJS Project

📝 Description

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.

🚀 Features

  • 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

🛠️ Tech Stack

  • Framework: NestJS
  • Database: PostgreSQL
  • ORM: TypeORM
  • Authentication: JWT + Passport
  • Validation: class-validator, class-transformer
  • Password Hashing: bcrypt

📁 Project Structure

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

🔧 Installation

  1. Clone the repository
cd blog-api
  1. Install dependencies
npm install
  1. Configure environment variables

    • Copy .env and update with your database credentials
    • Update JWT_SECRET with a secure random string
  2. Setup PostgreSQL Database

# Create database
createdb blog_db

# Or using psql
psql -U postgres
CREATE DATABASE blog_db;

🏃 Running the Application

# Development mode
npm run start:dev

# Production mode
npm run build
npm run start:prod

The API will be available at: http://localhost:3000/api/v1

📚 API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - Login user
  • GET /api/v1/auth/profile - Get current user profile (Protected)

Users

  • GET /api/v1/users - Get all users (Protected)
  • GET /api/v1/users/:id - Get user by ID (Protected)
  • POST /api/v1/users - Create user
  • PATCH /api/v1/users/:id - Update user (Protected)
  • DELETE /api/v1/users/:id - Delete user (Protected)

Posts

  • GET /api/v1/posts - Get all posts
  • GET /api/v1/posts/:id - Get post by ID
  • GET /api/v1/posts/author/:authorId - Get posts by author
  • POST /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)

🔐 Authentication

The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:

  1. Register or login to get an access token
  2. Include the token in the Authorization header:
Authorization: Bearer <your-token>

📝 Example Requests

Register

curl -X POST http://localhost:3000/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "username": "johndoe",
    "password": "password123"
  }'

Login

curl -X POST http://localhost:3000/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

Create Post

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
  }'

🧪 Testing

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

📦 Environment Variables

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

🏗️ Architecture Highlights

  • 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

📄 License

This project is licensed under the MIT License.

👨‍💻 Author

Built with ❤️ using NestJS

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published