Skip to content

A secure and fully functional RESTful Blog API built with Node.js, Express, and MongoDB. It implements JWT-based authentication for user signup and login, ensuring safe access control. Authenticated users can create, read, update, and delete their own blog posts, while all published blogs remain publicly accessible to every user.

Notifications You must be signed in to change notification settings

sanju1098/blog-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blog App API

A small REST API for a blog backend that supports user registration, authentication (JWT), and CRUD operations for posts.

Base URL: http://localhost:5000/api


Authentication

All protected endpoints require an Authorization header with a valid JWT in the form:

Authorization: Bearer

1) Register

  • Method: POST
  • Endpoint: /auth/register
  • Body (application/json):
{
  "name": "username",
  "email": "username@example.com",
  "password": "password123"
}
  • Success response:
{
  "success": true,
  "user": {
    "id": "691304cf9de42bb85b363089",
    "name": "username",
    "email": "username@example.com",
    "token": "<jwt-token>"
  }
}

2) Login

  • Method: POST
  • Endpoint: /auth/login
  • Body (application/json):
{
  "email": "username@example.com",
  "password": "password123"
}
  • Success response:
{
  "success": true,
  "user": {
    "id": "691304cf9de42bb85b363089",
    "name": "username",
    "email": "username@example.com",
    "token": "<jwt-token>"
  }
}

Posts

Public and protected endpoints to create, read, update and delete posts.

3) Create Post (authenticated)

  • Method: POST
  • Endpoint: /posts
  • Headers: Authorization: Bearer <token>, Content-Type: application/json
  • Body:
{
  "title": "My first blog",
  "body": "This is the body"
}
  • Success response:
{
  "title": "My first blog",
  "body": "This is the body",
  "author": "691304cf9de42bb85b363089",
  "_id": "691305509de42bb85b36308c",
  "createdAt": "2025-11-11T09:43:44.899Z",
  "updatedAt": "2025-11-11T09:43:44.899Z",
  "__v": 0
}

4) Get all posts (public)

  • Method: GET
  • Endpoint: /posts
  • Success response:
[
  {
    "_id": "691305509de42bb85b36308c",
    "title": "My first blog",
    "body": "This is the body",
    "author": {
      "_id": "691304cf9de42bb85b363089",
      "name": "username",
      "email": "username@example.com"
    },
    "createdAt": "2025-11-11T09:43:44.899Z",
    "updatedAt": "2025-11-11T09:43:44.899Z",
    "__v": 0
  },
  {
    "_id": "691302839de42bb85b363081",
    "title": "My first blog",
    "body": "This is the body",
    "author": {
      "_id": "6912ff694b19de0cf779766b",
      "name": "Sanjay",
      "email": "sanjay@example.com"
    },
    "createdAt": "2025-11-11T09:31:47.539Z",
    "updatedAt": "2025-11-11T09:31:47.539Z",
    "__v": 0
  }
]

5) Update post (only author)

  • Method: PUT
  • Endpoint: /posts/<postId>
  • Headers: Authorization: Bearer <token>, Content-Type: application/json
  • Body: partial or full (example):
{
  "title": "My first blog updated"
}
  • Success response:
{
  "_id": "691305509de42bb85b36308c",
  "title": "My first blog updated",
  "body": "This is the body",
  "author": "691304cf9de42bb85b363089",
  "createdAt": "2025-11-11T09:43:44.899Z",
  "updatedAt": "2025-11-11T09:45:04.602Z",
  "__v": 0
}

6) Delete post (only author)

  • Method: DELETE
  • Endpoint: /posts/<postId>
  • Headers: Authorization: Bearer <token>
  • Success response:
{
  "message": "Post deleted successfully"
}

About

A secure and fully functional RESTful Blog API built with Node.js, Express, and MongoDB. It implements JWT-based authentication for user signup and login, ensuring safe access control. Authenticated users can create, read, update, and delete their own blog posts, while all published blogs remain publicly accessible to every user.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published