Skip to content

bhutuklearning/The-Blog-App

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

The-Blog-App

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:

📋 Table of Contents

🔍 Project Overview

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.

✨ Features

  • 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

🛠️ Tech Stack

  • 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

📁 Project Structure

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

🔌 API Endpoints

Authentication

  • POST /api/v1/auth/register - Register a new user
  • POST /api/v1/auth/login - Login a user
  • POST /api/v1/auth/logout - Logout a user
  • GET /api/v1/auth/profile - Get authenticated user's profile

Users

  • GET /api/v1/users/profile - Get current user's profile
  • PUT /api/v1/users/completeprofile - Update user profile
  • GET /api/v1/users/:id - Get user by ID

Blogs

  • GET /api/v1/blogs - Get all blogs (with optional search parameter)
  • GET /api/v1/blogs/:id - Get blog by ID
  • GET /api/v1/blogs/my/blogs - Get blogs of logged-in user
  • POST /api/v1/blogs - Create a new blog
  • PUT /api/v1/blogs/:id - Update a blog
  • DELETE /api/v1/blogs/:id - Delete a blog

Blog Interactions

  • PUT /api/v1/blogs/:id/like - Like a blog
  • PUT /api/v1/blogs/:id/dislike - Dislike a blog
  • POST /api/v1/blogs/:id/comment - Add a comment to a blog
  • PUT /api/v1/blogs/:id/comment/:commentId - Edit a comment
  • DELETE /api/v1/blogs/:id/comment/:commentId - Delete a comment

🔐 Authentication

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:

  1. User registers or logs in
  2. Server generates a JWT token and sets it as a cookie
  3. Protected routes check for the token in cookies or Authorization header
  4. If valid, the user is authenticated and can access protected resources

📊 Models

User Model

  • 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

Blog Model

  • 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

🚀 Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB

Installation

  1. Clone the repository
  2. Install dependencies:
    cd backend
    npm install
  3. Create a .env file in the root directory with the required environment variables
  4. Start the development server:
    npm run backend

🔧 Environment Variables

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

Connect with me on:

About

The Blog App. This is the second entry of Backend Diaries.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published