GithubHelp home page GithubHelp logo

blog-backend's Introduction

Blog Backend API

This project implements a backend service for a blog platform, allowing users to perform CRUD operations on blog posts and manage user accounts. The backend is built using Node.js with the Express.js framework and uses MongoDB for data storage. Authentication is handled using token-based authentication (JWT).

Technologies Used

  • Backend Framework: Express.js (Node.js)
  • Database: MongoDB
  • Authentication: JSON Web Tokens (JWT)

Requirements

  1. API Endpoints:
    • Create a new blog post
    • Retrieve a list of all blog posts
    • Retrieve a single blog post by its ID
    • Retrieve blog posts by user ID
    • Update an existing blog post
    • Delete a blog post
    • Create, update, delete, and retrieve user details
  2. Data Validation:
    • Validate input data to ensure it meets the specified requirements (e.g., title and content are required fields).
  3. Authentication:
    • Secure the API endpoints using token-based authentication.
    • Users must authenticate before performing any CRUD operations on blog posts and user details.

Installation

  1. Clone the repository:

    git clone https://github.com/Deepika0701/Blog-Backend.git
    cd Blog-Backend
  2. Install dependencies:

    npm install
  3. Set up the MongoDB database and update the DATABASE_URL in .env file:

    MONGODB_URL={Write your url}
  4. Start the server:

    npm start

API Endpoints

User Management

  • Update User

    • URL: /api/users/:id
    • Method: PUT
    • Headers:
      {
        "Authorization": "Bearer your_jwt_token"
      }
    • Request Body (Example):
      {
        "username": "newUsername",
        "password": "newPassword"
      }
    • Response:
      {
        "id": "user_id",
        "username": "newUsername",
        ...
      }
  • Delete User

    • URL: /api/users/:id
    • Method: DELETE
    • Headers:
      {
        "Authorization": "Bearer your_jwt_token"
      }
    • Response:
      {
        "message": "User has been deleted successfully"
      }
  • Get User

    • URL: /api/users/:id
    • Method: GET
    • Response:
      {
        "id": "user_id",
        "username": "username",
        ...
      }

Blog Posts

  • Create a New Blog Post

    • URL: /api/posts/create
    • Method: POST
    • Headers:
      {
        "Authorization": "Bearer your_jwt_token"
      }
    • Request Body:
      {
        "title": "Blog Post Title",
        "content": "This is the content of the blog post.",
        "userId": "user_id"
      }
    • Response:
      {
        "id": "post_id",
        "title": "Blog Post Title",
        "content": "This is the content of the blog post.",
        "userId": "user_id",
        "createdAt": "2024-06-15T00:00:00.000Z",
        "updatedAt": "2024-06-15T00:00:00.000Z"
      }
  • Update an Existing Blog Post

    • URL: /api/posts/:id
    • Method: PUT
    • Headers:
      {
        "Authorization": "Bearer your_jwt_token"
      }
    • Request Body:
      {
        "title": "Updated Blog Post Title",
        "content": "This is the updated content of the blog post."
      }
    • Response:
      {
        "id": "post_id",
        "title": "Updated Blog Post Title",
        "content": "This is the updated content of the blog post.",
        "userId": "user_id",
        "createdAt": "2024-06-15T00:00:00.000Z",
        "updatedAt": "2024-06-15T00:00:00.000Z"
      }
  • Delete a Blog Post

    • URL: /api/posts/:id
    • Method: DELETE
    • Headers:
      {
        "Authorization": "Bearer your_jwt_token"
      }
    • Response:
      {
        "message": "Post has been deleted successfully"
      }
  • Get Post Details

    • URL: /api/posts/:id
    • Method: GET
    • Response:
      {
        "id": "post_id",
        "title": "Blog Post Title",
        "content": "This is the content of the blog post.",
        "userId": "user_id",
        "createdAt": "2024-06-15T00:00:00.000Z",
        "updatedAt": "2024-06-15T00:00:00.000Z"
      }
  • Get All Posts

    • URL: /api/posts
    • Method: GET
    • Query Parameters (Optional):
      • search: Search term to filter posts by title
    • Response:
      [
        {
          "id": "post_id",
          "title": "Blog Post Title",
          "content": "This is the content of the blog post.",
          "userId": "user_id",
          "createdAt": "2024-06-15T00:00:00.000Z",
          "updatedAt": "2024-06-15T00:00:00.000Z"
        },
        ...
      ]
  • Get Posts by User ID

    • URL: /api/posts/user/:userId
    • Method: GET
    • Response:
      [
        {
          "id": "post_id",
          "title": "Blog Post Title",
          "content": "This is the content of the blog post.",
          "userId": "user_id",
          "createdAt": "2024-06-15T00:00:00.000Z",
          "updatedAt": "2024-06-15T00:00:00.000Z"
        },
        ...
      ]

Data Validation

  • Ensure that the title and content fields are present in the request body when creating or updating a blog post.
  • Return a 400 Bad Request status code with an appropriate error message if the validation fails.

Authentication

  • Implement token-based authentication using JWT.
  • Protect all CRUD operations on blog posts and user details by requiring a valid JWT in the Authorization header.

Alt text Alt text Alt text Alt text Alt text Alt text Alt text Alt text Alt text Alt text Alt text

blog-backend's People

Contributors

deepika0701 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.