A RESTful API built with Express.js for managing a blog platform with authentication, posts, comments, and categories.
- User authentication (signup/login) with JWT
- Post management (create, read, list, filter by tags/categories)
- Comment system
- Category management
- Role-based access (admin, author, reader)
- MongoDB integration with Mongoose
- Redis caching for improved performance
- AI-powered content generation
- Input validation
- Protected routes with Passport.js
- Comprehensive test suite
- Docker containerization
- Node.js
- MongoDB
- npm or yarn
- Clone the repository
git clone [repository-url]
cd blog-expressjs-api
- Create a
.env
file (optional, default values are provided in docker-compose.yml)
PORT=3001
MONGO_URI=mongodb://mongodb:27017
MONGO_DB_NAME=blog
JWT_SECRET=your_jwt_secret
- Build and start the containers
# Build and start all services in detached mode
docker-compose up -d
# View the logs
docker-compose logs -f web
- Verify the installation
- API Server: http://localhost:3001
- MongoDB Express UI: http://localhost:8081
# Stop all services
docker-compose down
# Rebuild and restart services
docker-compose up -d --build
# View logs of specific service
docker-compose logs -f [service-name] # web, mongodb, redis, or mongo-express
# Reset data (removes volumes)
docker-compose down -v
- If ports are already in use:
# Check which process is using the port
lsof -i :3001
# or
netstat -ano | grep 3001
# Kill the process
kill -9 <PID>
- If MongoDB fails to start:
# Remove existing volume
docker-compose down -v
# Restart services
docker-compose up -d
- Check service status:
docker-compose ps
- Clone the repository
git clone [repository-url]
cd blog-expressjs-api
- Install dependencies
npm install
- Create a
.env
file in the root directory with the following variables:
PORT=3000
MONGO_URI=mongodb://localhost:27017
MONGO_DB_NAME=blog_db
JWT_SECRET=your_jwt_secret
Development mode:
npm run dev
Production mode:
npm start
- Authentication (signup/login)
- Post operations (CRUD)
- Comment management
- Category management
- Protected routes
- Input validation
- Error handling
- Cache operations
- POST
/api/v1/auth/signup
- Register a new user- Required fields: firstName, lastName, email, password, role
- POST
/api/v1/auth/login
- Login user- Required fields: email, password
- Returns: JWT token
- GET
/api/v1/posts
- List all posts - GET
/api/v1/posts/:id
- Get single post - POST
/api/v1/posts
- Create new post- Required fields: title, content, categories, tags, author
- GET
/api/v1/posts?tags=[]&categories=[]
- Filter posts - PUT
/api/v1/posts/:id/published
- Publish a post - PUT
/api/v1/posts/:id/draft
- Move post to draft - POST
/api/v1/posts/generate
- Generate AI content- Required field: category
- POST
/api/v1/posts/:postId/comments
- Add comment- Required fields: content, author
- GET
/api/v1/posts/:postId/comments
- Get comments - DELETE
/api/v1/posts/:postId/comments/:commentId
- Delete comment
- GET
/api/v1/categories
- List categories - POST
/api/v1/categories
- Create category- Required field: label
- PUT
/api/v1/categories/:id
- Update category - DELETE
/api/v1/categories/:id
- Delete category
Redis caching implementation:
- Post data cached for 1 hour by default
- Automatic cache invalidation on updates
- Cached data includes:
- Individual posts
- Post listings
- Filtered results
- Cache keys format:
{prefix}:{identifier}
The API includes AI-powered content generation:
- Generates blog posts based on categories
- Creates SEO-friendly titles
- Includes relevant tags
- Structures content with proper formatting
- Returns JSON-formatted content ready for posting
blog-expressjs-api/
├── src/
│ ├── controllers/ # Route controllers
│ ├── middleware/ # Custom middleware
│ ├── mongoose/ # Database models and schemas
│ ├── router/ # Route definitions
│ ├── services/ # Business logic
│ ├── strategies/ # Passport.js strategies
│ ├── test/ # Test suites
│ ├── utils/ # Utility functions
│ ├── index.js # App configuration
│ └── server.js # Server entry point
├── docker-compose.yml # Docker services configuration
├── Dockerfile # API container configuration
├── .env # Environment variables
└── package.json # Project dependencies
- Express.js - Web framework
- MongoDB with Mongoose - Database
- Redis - Caching layer
- JWT - Authentication
- Passport.js - Route protection
- Express Validator - Input validation
- Bcrypt - Password hashing
- Jest - Testing framework
- Supertest - HTTP testing
- MongoDB Memory Server - Testing database
- Ollama - AI content generation
- Docker - Containerization
- Docker Compose - Multi-container orchestration
The API uses standard HTTP response codes:
- 200: Success
- 201: Created
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden
- 404: Not Found
- 500: Internal Server Error
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
ISC
The application runs with the following services:
- API Server: Express.js application (Port 3001)
- MongoDB: Database (Port 27017)
- Redis: Caching layer (Port 6379)
- Mongo Express: Database management UI (Port 8081)