Blog API Application Project

Technologies

  • bcrypt
  • cloudinary
  • cors
  • dotenv
  • express
  • express-async-errors
  • express-mongo-sanitize
  • express-rate-limit
  • helmet
  • hpp
  • http-status-codes
  • jsonwebtoken
  • mongoose
  • multer
  • multer-storage-cloudinary
  • nodemon
  • validator
  • xss-clean
  • cookie-parser

API FEATURES

  • Authentication & Authorization
  • Post CRUD operations
  • User CRUD operations
  • Comment CRUD operations
  • Category CRUD operations
  • like and dislike a comment
  • Admin can suspend and unsuspend a user
  • A user can block different users
  • A user who block another user cannot see his/her posts
  • blocked user cannot see the profile of the user who blocked him/her
  • blocked user cannot follow the user who blocked him/her
  • blocked user cannot like, dislike or comment the post of the user who blocked him/her
  • A user can like and dislike a post
  • total likes and dislikes count
  • Changing user award base on number of posts created by the user
  • A user can follow and unfollow another user
  • Get following and followers count
  • Get posts of following users
  • A user can view another user's profile
  • Get total profile viewers count
  • Get posts created count
  • Get blocked counts
  • Get all users who views someone's profile
  • Update password
  • Profile photo uploaded
  • Upload images with posts
  • A user can delete his/her account
  • Using refresh token to get a new access token
  • Access token stored in a cookie

ENDPOINTS

Run Locally

Clone the project

  git clone https://link-to-project

Go to the project directory

  cd my-project

Install dependencies

  npm install

Start the server

  npm run server

Environment Variables

To run this project, you will need to add the following environment variables to your .env file

MONGODB_URL JWT_SECRET JWT_EXPIRE JWT_COOKIE_EXPIRES_IN

API Authentication

Some endpoints may require authentication for example. To create a create/delete/update post, you need to register your API client and obtain an access token.

The endpoints that require authentication expect a bearer token sent in the Authorization header.

Example:

Authorization: Bearer YOUR TOKEN

User API Reference

Register a new API client

POST /api/v1/users/register

The request body needs to be in JSON format.

User Login

POST /api/v1/users/login
Parameter Type Description Required
authentication string Your token no
email string Your email yes
password string Your password yes

Example request body:

{
  "email":"your email"
  "password":"your password"
}

Refresh Token

POST /api/v1/users/refresh-token
Body Type Description Required
JWT refresh token string Your token yes

Example request body:

{
  "refreshToken":"your refresh token"
}

get my profile

GET /api/v1/users/profile
Parameter Type Description Required
authentication string Your token yes

Get all users

GET /api/v1/users/users
Parameter Type Description Required
authentication string Your token no

view a user profile

GET /api/v1/users/view-profile/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the user you want to view his profile yes

Following a user

POST /api/v1/users/follow-user/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the user you want to follow yes

UnFollowing a user

POST /api/v1/users/unfollow-user/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the user you want to follow yes

Update user password

PATCH /api/v1/users/update-password
Parameter Type Description Required
authentication string Your token yes
password string Enter your password yes

Example request body:

{
  "password":"value"
}

Update your profile

PATCH /api/v1/users/update-me
Parameter Type Description Required
authentication string Your token yes
email string Enter your email no
firstname string Enter your firstname no
lastname string Enter your lastname no

Example request body:

{
  "email":"value",
  "firstname":"value",
  "lastname":"value",
}

Block another user

PATCH /api/v1/users/block-user/:id
Parameter Type Description Required
authentication string Your token yes
id string Id of the user you want to block yes

Unblock user

PATCH /api/v1/users/unblock-user/:id
Parameter Type Description Required
authentication string Your token yes
id string Id of the user you want to unblock yes

Admin suspend a user

PATCH /api/v1/users/suspend-user/:id
Parameter Type Description Required
authentication string Your token yes
id string Id of the user you want to block yes

Admin unsuspend a user

PATCH /api/v1/users/unsuspend-user/:id
Parameter Type Description Required
authentication string Your token yes
id string Id of the user you want to unblock yes

Delete your account

  DELETE /api/v1/users/delete-me
Parameter Type Description Required
authentication string Your token yes

Upload Profile Photo

  POST /api/v1/users/profile-photo-upload
Parameter Type Description Required
authentication string Your token yes
profilePhoto string Image to upload yes

Posts API Reference

Create Post

  POST /api/v1/posts
Parameter Type Description Required
authentication string Your token yes
title string Post title yes
description string Post description yes
category string Name of the category no
photo string Image of the post no

Example request body:

{
  "title":"value",
  "description":"value",
  "category":"value",
  "photo":"photo",
}

Get the feed

  GET /api/v1/posts
Parameter Type Description Required
authentication string Your token no

Get posts of followings

  GET /api/v1/posts/posts-of-following-user
Parameter Type Description Required
authentication string Your token yes

Get Single Post

  GET /api/v1/posts/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes

Like Post

  GET /api/v1/like-post/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes

Dislike Post

  GET /api/v1/posts/dislike-post/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes

Update Post

  PATCH /api/v1/posts/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes
title string title of the post yes
description string description of the post yes
category string category of the post no
photo string photo of the post no

Example request body:

{
  "title":"value",
  "description":"value",
  "category":"value",
  "photo":"photo",
}

Delete Post

  DELETE /api/v1/posts/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes

Comment API Reference

Create Comment

  POST /api/v1/comments
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes

Delete Comment

  DELETE /api/v1/comments/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the comment yes

Update Comment

  PUT /api/v1/comments/:id
Parameter Type Description Required
authentication string Your token yes
id string ID of the post yes