Ali-Amir-code / t12-blog-api

Repository from Github https://github.comAli-Amir-code/t12-blog-apiRepository from Github https://github.comAli-Amir-code/t12-blog-api

Blogging Platform RESTful API

A back-end RESTful API for a blogging platform built with Node.js, Express, and MongoDB. Supports user authentication, blog post management and comments.

Features

  • User Management
    Register/login with JWT authentication. Password hashing via bcryptjs.
  • Blog Posts
    Full CRUD operations. Posts include title, content and author. Users can only modify their own posts.
  • Comments
    Add/delete comments on posts. Fetch all comments for a post.
  • Engagement
    Like/unlike functionality for posts.
  • API Structure
    RESTful principles with logical endpoints.
  • Database
    MongoDB storage using Mongoose ODM.
  • Pagination
    Implemented for posts and comments.

Tech Stack

Category Technologies
Backend Node.js, Express
Database MongoDB (Mongoose)
Auth JWT, bcryptjs
Environment dotenv

Prerequisites

  • Node.js (v14+)
  • MongoDB (local or cloud instance)

Setup & Installation

  1. Clone the repository:

    git clone https://github.com/ali-amir-code/t12-blog-api.git
    cd t7-blog-api
  2. Install dependencies:

    npm install
  3. Configure environment: Create .env file with:

    PORT=5000
    MONGO_URI=mongodb://localhost:27017/blogging-platform
    JWT_SECRET=your_strong_secret_here
  4. Start the server:

    node server.js

    API runs at http://localhost:5000

API Endpoints

User Routes (/api/users)

Method Endpoint Description Auth
POST /register Register new user No
POST /login Authenticate user No

Post Routes (/api/posts)

Method Endpoint Description Auth
POST / Create new post Yes
GET / Get all posts No
GET /:id Get single post No
PUT /:id Update post Yes
DELETE /:id Delete post Yes
PUT /:id/like Like/unlike post Yes

Comment Routes (/api/comments)

Method Endpoint Description Auth
POST /:postId Add comment to post Yes
GET /:postId Get comments for post No
DELETE /:id Delete comment Yes

Note: All authenticated routes require JWT in Authorization: Bearer <token> header.

Example Requests

User Registration

curl -X POST http://localhost:5000/api/users/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "aliamir",
    "email": "ali@example.com",
    "password": "password123"
  }'

User Login

curl -X POST http://localhost:5000/api/users/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "ali@example.com",
    "password": "password123"
  }'

Create Post with Image

curl -X POST http://localhost:5000/api/posts \
  -H "Authorization: Bearer <your_jwt_token>" \
  -F "title=Post Title" \
  -F "content=Post content" \

About


Languages

Language:JavaScript 100.0%