Skip to content

A simple and secure REST API built with Node.js, MongoDB, Express, and JWT for user authentication and creation of posts. Manage user registration, login, logout, and password reset as well as post management seamlessly

Notifications You must be signed in to change notification settings

MeetMulik/Rest-Api-Nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST-Api

A simple RESTful API built using Node.js, MongoDB, Express, and JWT for user authentication and post management(CRUD).

Table of Contents

Introduction

A simple RESTful API built using Node.js, MongoDB, Express, and JWT for user authentication and post management(CRUD).

Features

  • User registration (signup)
  • User authentication (login)
  • User logout
  • Password reset functionality
  • View & Update user profile
  • Create Posts - (Create, Read, Update, Delete) Posts
  • Add comments on posts
  • Delete comments
  • Swagger Documentation

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Node.js installed
  • MongoDB server running
  • npm or yarn installed

Installation

  1. Clone the repository:
git clone https://github.com/MeetMulik/Rest-Api-Meet.git
  1. Install dependencies:
cd Rest-Api-Meet
npm install
  1. Set up environment variables: Create a .env file in the root of your project and add the following:
PORT=
MONGO_URI=
JWT_SECRET=
ADMIN_EMAIL=
ADMIN_PASSWORD=

Usage

1.To start the server, run the following command:

npm run dev

API Endpoints

Swagger Docs

To view the Swagger documentation for the API, navigate to http://localhost:5000/api-docs/ after starting the application.

All post-related routes are prefixed with /api/users.

1. Signup

  • Method: POST
  • Endpoint: /signup
  • Description: Registers a new user.
  • Request Body:
    {
      "name": "",
      "username": "",
      "email": "",
      "password": ""
    }
  • Response:
    {
      "_id": "",
      "name": "",
      "username": "",
      "email": ""
    }

2. Login

  • Method: POST
  • Endpoint: /login
  • Description: Logs in an existing user.
  • Request Body:
{
  "username": "",
  "password": ""
}
  • Response: { "_id": "", "name": "", "username": "", "email": "" }

3. Logout

  • Method: POST
  • Endpoint: /logout
  • Description: Logs the user out.

4. Forget Password

  • Method: POST
  • Endpoint: /forget-password
  • Description: Initiates the password reset process.
  • Request Body:
    {
      "email": ""
    }
  • Response: (No specific response body; initiates the password reset process and sends an email with instructions)

5. Reset Password

  • Method: POST
  • Endpoint: /reset-password
  • Description: Resets the user's password.
  • Request Body:
    {
      "password": ""
    }
  • Response: (No specific response body; successful password reset)

6. Get Profile By Username

  • Method: GET
  • Endpoint: /profile/:username
  • Description: Retrieves the profile information of a user by username.

6. Update Profile By ID

  • Method: PATCH
  • Endpoint: /profile/:id
  • Description: Updates the profile information of a user by ID.
  • Middleware: protectRoute
  • Request Body:
    {
      "username": ""
    }
    //Or any property to changed

Post Routes

All post-related routes are prefixed with /api/posts.

1. Get All Posts

  • Method: GET
  • Endpoint: /
  • Description: Retrieves all posts.

2. Get Post by ID

  • Method: GET
  • Endpoint: /:postId
  • Description: Retrieves a specific post by its ID.

3. Get Post by UserID

  • Method: GET
  • Endpoint: /user/:userId
  • Description: Retrieves a specific post by its ID.

4. Create Post

  • Method: POST

  • Endpoint: /create

  • Description: Creates a new post.

  • Middleware: protectRoute

  • Request Body:

    {
      "text": "",
      "postImg": ""
    }

5. Update Post

  • Method: PATCH

  • Endpoint: /update/:postId

  • Description: Updates an existing post.

  • Middleware: protectRoute

  • Request Body:

    {
      "title": "",
      "content": ""
    }

6. Delete Post

  • Method: DELETE
  • Endpoint: /:postId
  • Description: Deletes a specific post.
  • Middleware: protectRoute

7. Add Comment

  • Method: POST

  • Endpoint: /comment/:postId

  • Description: Adds a comment to a specific post.

  • Middleware: protectRoute

  • Request Body:

    {
      "comment": ""
    }

7. Delete Comment

  • Method: DELETE
  • Endpoint: /:postId/comment/:commentId
  • Description: Deletes a comment associated with a post.
  • Middleware: protectRoute

About

A simple and secure REST API built with Node.js, MongoDB, Express, and JWT for user authentication and creation of posts. Manage user registration, login, logout, and password reset as well as post management seamlessly

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published