Giter VIP home page Giter VIP logo

blog-app's Introduction

API Documentation

Overview

This API provides the following functionality for user registration, authentication, and blog post management:

  • Register a new user
  • Login an existing user
  • Create a username for the user
  • Create a new blog post
  • Retrieve a blog post by author and post ID
  • Update a blog post
  • Delete a blog post
  • Upload an image
  • Get an image

Base URL

The base URL for all endpoints is: http://localhost:3001/api

Environment Variables

To configure the API, you can create a .env file in the root directory of your project. In this file, you should define the following environment variables:

  • MONGODB_URL: Mongodb database url
  • JWT_SECRET: The JSON Web Token secret used for authentication.
  • JWT_EXPIRES_IN: The expiration date for JSON Web Tokens (JWT).

Here's an example of how your .env file should look: MONGODB_URL=mongodb://localhost:27017/blog JWT_SECRET=your_jwt_secret_here JWT_EXPIRES_IN=30d

Ensure these environment variables are correctly configured in your .env file before running the API.

Authentication

To access protected routes, include the JWT token in the Authorization header as follows: Authorization: Bearer token

Please note that the following routes do not require the JWT token in the Authorization header:

  • POST /auth/register/signup
  • POST /auth/register/login

Endpoints

Register a New User

POST /auth/register/signup

  • Register a new user with the provided body containing email and password.

Body

{ "email": "[email protected]", "password": "password123" }

Success Response

{ "id": "userID" }

Error Response

{
  "statusCode": "error status code",
  "message": "error message",
  "error": "error name"
}

Login an Existing User

POST /auth/register/login

  • Login an existing user with the provided Body containing email and password.

Body

{ "email": "[email protected]", "password": "password123" }

Success Response

{ "token": "jwt_access_token" }

Error Response

{
  "statusCode": "error status code",
  "message": "error message",
  "error": "error name"
}

Create a Username for the User

POST /auth/username

  • Create or change a username for the user. This username can be associated with the user's blog posts. This route is protected by JWT authentication.

Body

{ "username": "user123", "password": "password123" }

Success Response

{ "id": "user_id" }

Error Response

{
  "statusCode": "error status code",
  "message": "error message",
  "error": "error name"
}

Create a New Blog Post

PUT /post/new

  • Create a new blog post with the provided Body containing a title and content array according to the specified format.

Body

{ "title": "Blog Post Title", "content": [ { "type": "p", "value": "Lorem
      ipsum dolor sit amet, consectetur adipiscing elit."
    },
    { "type": "span", "value": "Hello world"
    },
    { "type": "image", "alt": "Image alt", "value": "image url"
    },
    { "type": "code", "code": "code block url"
    },
    { "type": "url", "value": "Click me", "url": "https://google.com"
    }
  ]
}

Success Response

{
  "title": "Blog Post Title",
  "id": "post_id",
  "author": "username",
  "createdAt": "date",
  "lastModified": "date",
  "content": [
    {
      "type": "p",
      "value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    },
    { "type": "span", "value": "Hello world" },
    { "type": "image", "alt": "Image alt", "value": "image url" },
    { "type": "code", "code": "code block url" },
    { "type": "url", "value": "Click me", "url": "https://google.com" }
  ]
}

Error Response

{
  "statusCode": "error status code",
  "message": "error message",
  "error": "error name"
}

Retrieve a Blog Post by Author and Post ID

GET /post/get/:author/:postid

  • Retrieve a blog post by author and post ID. This route does not require a JWT token.

Success Response

{
  "title": "test",
  "id": "post_id",
  "author": "username",
  "createdAt": "date",
  "lastModified": "date",
  "content": ["post content"]
}

Update a Blog Post

PUT /post/update/:postid

  • Update a blog post with the provided Body containing new content. This route is protected by JWT authentication.

Body

{
  "content": [
    { "type": "p", "value": "Updated content" },
    { "type": "span", "value": "Hello updated world" }
  ]
}

Success Response

{ "message": "OK", "statusCode": 200 }

Error Response

{
  "statusCode": "error status code",
  "message": "error message",
  "error": "error name"
}

Delete a Blog Post

DELETE /post/delete/:postid

  • Delete a blog post by post ID. This route is protected by JWT authentication.

Success Response

{ "message": "OK", "statusCode": 200 }

Upload an Image

POST /post/upload

  • Upload an image file with the name attribute set to "file" and the content type or enctype set to "multipart/form-data". This route only accepts image files (.jpg, .PNG, .gif).

Success Response

{ "url": "image url or image path/filename" }

Get an Image

GET /post/image/:image_path_or_url

  • Retrieve an image by specifying its path or URL. This route does not require a JWT token.

Success Response

The image data is streamed to the user.

Please make sure to replace your_jwt_secret_here, 30d, [email protected], password123, userID, jwt_access_token, user123, user_id, Blog Post Title, post_id, username, image url, code block url, date, post content, Updated content, Hello updated world, image path/filename, and image_path_or_url with actual values when making requests.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

blog-app's People

Contributors

ohanronnie 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.