Skip to content

Mareola-Mabs/Blog-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ Blogging API

A RESTful API built with Node.js, Express, and MongoDB that allows users to create, manage, and publish blog posts.
This API supports JWT-based authentication, pagination, searching, filtering, and ordering of blog posts.


๐Ÿš€ Features

  • User authentication with JWT (expires after 1 hour)
  • Public and Private blog access
  • Blog states: draft and published
  • Pagination (default: 20 blogs per page)
  • Searchable by author, title, or tags
  • Orderable by read_count, reading_time, and timestamp
  • Auto-increment read_count when a blog is viewed
  • Reading time automatically calculated
  • CRUD operations for blog owners
  • Uses MVC pattern for clean architecture

๐Ÿ› ๏ธ Tech Stack

  • Backend Framework: Node.js + Express
  • Database: MongoDB + Mongoose
  • Authentication: JWT (JSON Web Token)
  • Environment Variables: dotenv
  • Logger: morgan
  • Security & CORS: cors
  • Testing: Jest / Supertest (optional)

๐Ÿ“ฆ Installation

1๏ธโƒฃ Clone the Repository

git clone https://github.com/<your-username>/blogging-api.git
cd blogging-api

2๏ธโƒฃ Install Dependencies

npm install

3๏ธโƒฃ Create a .env File

touch .env

Add the following:

PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_secret_key
NODE_ENV=development

โš ๏ธ Ensure your MongoDB connection string is correct.

โ–ถ๏ธ Running the Server

Development Mode

npm run dev

Production Mode

npm start

Server will run on:

http://localhost:5000

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ db.js            # MongoDB connection
โ”‚   โ””โ”€โ”€ jwt.js           # JWT helpers (sign/verify)
โ”œโ”€โ”€ controllers/
โ”‚   โ”œโ”€โ”€ blog.controller.js
โ”‚   โ””โ”€โ”€ user.controller.js
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ Blog.js
โ”‚   โ””โ”€โ”€ User.js
โ”œโ”€โ”€ routes/
โ”‚   โ”œโ”€โ”€ blog.routes.js
โ”‚   โ””โ”€โ”€ user.routes.js
โ”œโ”€โ”€ middlewares/
โ”‚   โ””โ”€โ”€ auth.middleware.js
โ”œโ”€โ”€ server.js
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md

๐Ÿ”‘ Authentication

This API uses JWT for authentication.

Login to Get Token

Once a user logs in, youโ€™ll receive a JWT token.

Add it to your headers for protected routes:

Authorization: Bearer <your_token>

Tokens expire after 1 hour.


๐Ÿง User Endpoints

1๏ธโƒฃ Register a User

POST /users/signup

Request Body:

{
  "first_name": "Mareola",
  "last_name": "Plate",
  "email": "mareola@example.com",
  "password": "123456"
}

Response:

{
  "message": "User registered successfully",
  "token": "<jwt_token>"
}

2๏ธโƒฃ Login User

POST /users/login

Request Body:

{
  "email": "mareola@example.com",
  "password": "123456"
}

Response:

{
  "message": "Login successful",
  "token": "<jwt_token>"
}

๐Ÿ“ฐ Blog Endpoints

1๏ธโƒฃ Get All Published Blogs (Public)

GET /blogs?state=published&page=1&limit=20&author=mike&order_by=read_count

Supports:

  • Pagination โ†’ page, limit
  • Filtering โ†’ state
  • Searching โ†’ author, title, tags
  • Ordering โ†’ read_count, reading_time, timestamp

2๏ธโƒฃ Get a Single Blog (Public) (Blog must have been published using the PATCH/:id/state route)

GET /blogs/:id

Automatically:

  • Increments read_count by 1
  • Returns author details

Response:

{
  "title": "How to Learn Node.js",
  "description": "A beginner guide",
  "author": {
    "first_name": "Mareola",
    "last_name": "Plate"
  },
  "read_count": 45,
  "reading_time": "3 mins",
  "state": "published",
  "tags": "nodejs", "backend",
  "body": "Full blog content..."
}

3๏ธโƒฃ Create a Blog (Authenticated)

POST /blogs

Header:

Authorization: Bearer <token>

Body:

{
  "title": "My First Blog",
  "description": "Introduction to blogging",
  "tags": "intro", "blog",
  "body": "This is my first blog post."
}

Created blogs start as draft.

4๏ธโƒฃ Publish a Blog (Authenticated)

PATCH /blogs/:id/state

Body:

{
  "state": "published"
}

5๏ธโƒฃ Edit a Blog (Authenticated, Owner Only)

PUT /blogs/:id

Body:

{
  "title": "Updated Blog Title",
  "body": "Updated blog content."
}

6๏ธโƒฃ Delete a Blog (Authenticated, Owner Only)

DELETE /blogs/:id

Deletes the blog completely from the database.

7๏ธโƒฃ Get All Blogs by Logged-in User

GET /blogs/my-blogs?state=draft&page=1

Lists blogs created by the logged-in user.


๐Ÿงฎ Reading Time Algorithm

The reading time is estimated based on word count:

const wordsPerMinute = 200
const readingTime = Math.ceil(wordCount / wordsPerMinute)

Result stored as: "3 mins read".


๐Ÿงช Testing (Optional)

Run tests with:

npm test

You can use Jest or Supertest for endpoint testing.


โ˜๏ธ Deployment

For Render or Vercel deployment:

  1. Connect your GitHub repo.
  2. Add environment variables under project settings.
  3. Use the start command:
npm start

Render automatically detects Express and sets up a web service.


๐Ÿ‘จโ€๐Ÿ’ป Author

Mareola
Software Engineer | Backend Developer | Tech Innovator


๐Ÿง  License

MIT License ยฉ 2025 Mareola

About

A blog API with express.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published