Skip to content

krishna7054/BLOG-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blog API

This is a RESTful API for a simple blogging platform, built using Node.js, Express, MySQL, and JWT for authentication. The API allows users to perform CRUD operations (Create, Read, Update, Delete) on blog posts and includes token-based authentication for secure access.

Features

  • User Registration and Login
  • Create, Read, Update, and Delete Blog Posts
  • Token-Based Authentication

Technologies Used

  • Node.js
  • Express.js
  • MySQL
  • JWT (JSON Web Token)
  • bcryptjs

Getting Started

Prerequisites

  • Node.js installed on your machine
  • MySQL server running

Installation

  1. Clone the repository:
    git clone https://github.com/your-username/blog-api.git
    cd blog-api
  2. Install dependencies:
    npm install
  3. Set up the database:
    CREATE DATABASE blog;
    USE blog;
    
    CREATE TABLE users (
      id INT AUTO_INCREMENT PRIMARY KEY,
      username VARCHAR(255) NOT NULL,
      password VARCHAR(255) NOT NULL
    );
    
    CREATE TABLE posts (
      id INT AUTO_INCREMENT PRIMARY KEY,
      title VARCHAR(255) NOT NULL,
     content TEXT NOT NULL,
      user_id INT,
      FOREIGN KEY (user_id) REFERENCES users(id)
    );
    
  4. Run the server:
    node app.js

API Endpoints

Authentication

  • Register a new user
    • URL: /api/register
    • Method: POST
    • Body:
      {
      "username": "testuser",
      "password": "testpassword"
       }

image

  • Login and get a token
    • URL: /api/login
    • Method: POST
    • Body:
      {
      "username": "testuser",
      "password": "testpassword"
       }

image

Blog Posts

  • Create a new blog post
    • URL: /api/posts
    • Method: POST
    • Headers:
      • x-access-token: your_jwt_token
    • Body:
      {
      "title": "My First Post",
      "content": "This is the content of my first post."
       }

Screenshot 2024-06-11 223426

  • Retrieve a list of all blog posts
    • URL: /api/posts
    • Method: GET

Screenshot 2024-06-11 223458

  • Retrieve a single blog post by its ID
    • URL: /api/posts/:id
    • Method: GET

Screenshot 2024-06-11 223528

  • Update an existing blog post
    • URL: /api/posts/:id
    • Method: PUT
    • Headers:
      • x-access-token: your_jwt_token
    • Body:
      {
      "title": "Updated Post Title",
      "content": "This is the updated content."
       }

Screenshot 2024-06-11 223604

After update

Screenshot 2024-06-11 223628

  • Delete a blog post
    • URL: /api/posts/:id
    • Method: DELETE
    • Headers:
      • x-access-token: your_jwt_token

Screenshot 2024-06-11 223656

After Delete

Screenshot 2024-06-11 223730

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published