This is the second entry in the Backend Diaries series, The Blog App. The primary goal is to create a full-featured backend for a blog application with authentication, authorization, and rich text blog creation capabilities and along with that interacting with the blog app with likes and comments. All the API endpoints are tested on postman and postman collection file is also uploaded for the reference.
Connect with me on:
- LinkedIn: Amritanshu Goutam
- X(Twitter): Amritanshutwt
- Project Overview
- Features
- Tech Stack
- Project Structure
- API Endpoints
- Authentication
- Models
- Getting Started
- Environment Variables
The Blog App is designed to provide a robust backend for a modern blogging platform. The main intent is to involve more complexities and learn how to develop applications with advanced features like user authentication, content management, and social interactions.
- User Authentication: Register, login, and logout functionality with JWT
- User Profiles: View and update user profiles with social media links
- Blog Management: Create, read, update, and delete blog posts
- Interactions: Like, dislike, and comment on blog posts
- Search: Full-text search for blog posts
- Security: Password hashing, JWT authentication, and content sanitization
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JWT (JSON Web Tokens)
- Security: bcrypt for password hashing
- Content Sanitization: sanitize-html
backend/
├── package.json # Project dependencies and scripts
└── src/
├── config/ # Configuration files
│ └── db.js # Database connection setup
├── controller/ # Request handlers
│ ├── auth.controller.js # Authentication logic
│ ├── blog.controller.js # Blog operations
│ └── user.controller.js # User profile operations
├── middleware/ # Custom middleware
│ └── authMiddleware.js # JWT authentication middleware
├── models/ # Database schemas
│ ├── blog.model.js # Blog schema
│ └── user.model.js # User schema
├── routes/ # API routes
│ ├── auth.route.js # Authentication routes
│ ├── blog.route.js # Blog routes
│ └── user.route.js # User routes
├── utils/ # Utility functions
│ └── generateToken.js # JWT token generation
└── server.js # Main application entry point
POST /api/v1/auth/register
- Register a new userPOST /api/v1/auth/login
- Login a userPOST /api/v1/auth/logout
- Logout a userGET /api/v1/auth/profile
- Get authenticated user's profile
GET /api/v1/users/profile
- Get current user's profilePUT /api/v1/users/completeprofile
- Update user profileGET /api/v1/users/:id
- Get user by ID
GET /api/v1/blogs
- Get all blogs (with optional search parameter)GET /api/v1/blogs/:id
- Get blog by IDGET /api/v1/blogs/my/blogs
- Get blogs of logged-in userPOST /api/v1/blogs
- Create a new blogPUT /api/v1/blogs/:id
- Update a blogDELETE /api/v1/blogs/:id
- Delete a blog
PUT /api/v1/blogs/:id/like
- Like a blogPUT /api/v1/blogs/:id/dislike
- Dislike a blogPOST /api/v1/blogs/:id/comment
- Add a comment to a blogPUT /api/v1/blogs/:id/comment/:commentId
- Edit a commentDELETE /api/v1/blogs/:id/comment/:commentId
- Delete a comment
The API uses JWT (JSON Web Tokens) for authentication. The token is stored in an HTTP-only cookie for security. The authentication flow is as follows:
- User registers or logs in
- Server generates a JWT token and sets it as a cookie
- Protected routes check for the token in cookies or Authorization header
- If valid, the user is authenticated and can access protected resources
- name: User's full name
- email: User's email address (unique)
- password: Hashed password (not returned in queries)
- bio: User's biography
- socials: Social media links (LinkedIn, Instagram, X/Twitter)
- lastLogin: Timestamp of last login
- Virtual fields: blogs, blogCount, likedBlogs, totalLikesReceived, totalCommentsReceived
- title: Blog post title
- content: Blog post content (HTML sanitized)
- author: Reference to User model
- likes: Array of User IDs who liked the post
- dislikes: Array of User IDs who disliked the post
- comments: Array of comment objects with user reference, text, and timestamp
- Virtual fields: likeCount, dislikeCount, commentCount
- Node.js (v16 or higher)
- MongoDB
- Clone the repository
- Install dependencies:
cd backend npm install
- Create a
.env
file in the root directory with the required environment variables - Start the development server:
npm run backend
Create a .env
file in the root directory with the following variables:
PORT=10000
MONGO_URL=mongodb://localhost:27017/
DB_NAME=blog_app
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES=7d
NODE_ENV=development
Connect with me on:
- LinkedIn: Amritanshu Goutam
- X(Twitter): Amritanshutwt