Skip to content

shuvojoseph/BlogPostNestJS

Repository files navigation

Nest Logo

BlogPostNestJS 📝

A scalable, modular backend built with NestJS + MongoDB + TypeORM, featuring authentication, role-based ownership, and clean architecture.


🚀 Overview

BlogPostNestJS is a backend API built using the NestJS framework.
It provides authentication, user management, and a complete blog system with owners and co-owners (multi-user collaboration).

This project mirrors real-world enterprise architecture, including:

  • Role-based access control
  • MongoDB Atlas integration
  • Swagger API documentation
  • Unit testing using Jest
  • Clean modular code organization

🏗️ Features

  • ✅ User registration & authentication (JWT)
  • ✅ Blog creation, editing, and deletion
  • ✅ Multiple co-owners per blog post
  • ✅ Ownership guard for access control
  • ✅ Global validation & logging middleware
  • ✅ MongoDB + TypeORM integration
  • ✅ Swagger API documentation
  • ✅ Jest-based unit testing setup

🧱 Project Structure


src/
├── main.ts
├── app.module.ts
├── common/
│   ├── decorators/current-user.decorator.ts
│   ├── guards/jwt-auth.guard.ts
│   ├── guards/ownership.guard.ts
│   ├── middleware/logger.middleware.ts
│   └── pipes/objectid.pipe.ts
├── auth/
│   ├── auth.module.ts
│   ├── auth.controller.ts
│   ├── auth.service.ts
│   ├── jwt.strategy.ts
│   └── dtos/
│       └── register.dto.ts
├── users/
│   ├── users.module.ts
│   ├── users.controller.ts
│   ├── users.service.ts
│   ├── user.schema.ts
│   └── dtos/create-user.dto.ts
├── blogs/
│   ├── blogs.module.ts
│   ├── blogs.controller.ts
│   ├── blogs.service.ts
│   ├── blog.schema.ts
│   └── dtos/
│       ├── blog.dto.ts
│       └── upsert-blog.dto.ts
├── project-owners/
│   ├── project-owners.module.ts
│   └── project-owners.service.ts
└── entities/
├── blog.entity.ts
├── project-owner.entity.ts
└── user.entity.ts


⚙️ Installation & Setup

1️⃣ Install Nest CLI (if not already)

npm install -g @nestjs/cli
nest -v

2️⃣ Create & Run the Project

git clone https://github.com/shuvojoseph/BlogPostNestJS.git
cd BlogPostNestJS
npm install
npm run start

Then visit: 👉 http://localhost:3000


🗝️ Environment Configuration

Create a .env file in the project root:

PORT=3000
MONGODB_URI="your-mongodb-atlas-connection-string"
JWT_ACCESS_SECRET="dev_access_secret_change_me"
JWT_REFRESH_SECRET="dev_refresh_secret_change_me"
JWT_ACCESS_TTL="900s"  # 15 minutes
JWT_REFRESH_TTL="7d"   # 7 days

⚠️ Make sure not to commit .env to GitHub.


🧩 Swagger API Docs

Once the server is running, open your browser: 👉 http://localhost:3000/docs

Swagger is automatically configured using:

@nestjs/swagger
swagger-ui-express

🧠 MongoDB Setup

  1. Create a MongoDB Atlas cluster 📺 How to setup (YouTube)

  2. Allow your local IP (or 0.0.0.0/0 for testing)

  3. Connect using MongoDB Compass (optional)

  4. Update your .envMONGODB_URI accordingly


🧪 Unit Testing

Installed test dependencies:

npm install --save-dev jest @types/jest ts-jest
npx ts-jest config:init

NestJS recommends keeping test files next to the source:

src/blogs/blog.service.spec.ts

Run tests:

npm run test

Expected output:

PASS  src/blogs/blog.service.spec.ts
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total

🧰 Useful Commands

Command Description
npm run start Run the app in normal mode
npm run start:dev Run in watch mode (auto-reload)
npm run build Compile TypeScript to JS
npm run test Run unit tests
npm run test:cov Generate test coverage report

📚 Technologies Used

  • NestJS (modular Node.js framework)
  • MongoDB Atlas (NoSQL database)
  • TypeORM (ORM for MongoDB)
  • JWT (Authentication)
  • Passport (Strategy management)
  • Swagger (API documentation)
  • Jest (Testing framework)
  • Bcrypt (Password hashing)
  • Class-validator / Class-transformer

💡 Highlights

  • Clean, modular architecture
  • JWT-based authentication
  • Ownership & co-ownership model
  • Centralized validation & logging
  • Swagger for interactive API docs
  • Unit test coverage with Jest

👨‍💻 Author

Shuvo Joseph Founder — DeshiIT 🔗 LinkedIn


🪪 License

This project is MIT licensed.


⭐ If you found this project helpful, please star the repository!


About

A modular NestJS backend with MongoDB, JWT authentication, and co-ownership blog features.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published