A scalable, modular backend built with NestJS + MongoDB + TypeORM, featuring authentication, role-based ownership, and clean architecture.
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
- ✅ 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
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
npm install -g @nestjs/cli
nest -vgit clone https://github.com/shuvojoseph/BlogPostNestJS.git
cd BlogPostNestJS
npm install
npm run startThen visit: 👉 http://localhost:3000
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.envto GitHub.
Once the server is running, open your browser: 👉 http://localhost:3000/docs
Swagger is automatically configured using:
@nestjs/swagger
swagger-ui-express-
Create a MongoDB Atlas cluster 📺 How to setup (YouTube)
-
Allow your local IP (or
0.0.0.0/0for testing) -
Connect using MongoDB Compass (optional)
-
Update your
.env→MONGODB_URIaccordingly
Installed test dependencies:
npm install --save-dev jest @types/jest ts-jest
npx ts-jest config:initNestJS recommends keeping test files next to the source:
src/blogs/blog.service.spec.ts
Run tests:
npm run testExpected output:
PASS src/blogs/blog.service.spec.ts
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
| 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 |
- 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
- Clean, modular architecture
- JWT-based authentication
- Ownership & co-ownership model
- Centralized validation & logging
- Swagger for interactive API docs
- Unit test coverage with Jest
Shuvo Joseph Founder — DeshiIT 🔗 LinkedIn
This project is MIT licensed.
⭐ If you found this project helpful, please star the repository!