Skip to content

SALAH-NAME/BlogForge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Social Media API

BlogForge API is a robust, scalable RESTful API designed to power modern blogging platforms. This API supports user authentication, post management, commenting, following system, and more.

πŸš€ Features

  • πŸ” User authentication (JWT-based)
  • πŸ“ Post creation and management
  • πŸ’¬ Commenting system
  • πŸ‘₯ Follow/Unfollow functionality
  • πŸ” Search capabilities for posts and users
  • πŸ“Š Rate limiting
  • πŸ›‘οΈ Security features (XSS protection, NoSQL injection prevention)

πŸ“‹ Prerequisites

  • Node.js (v14.0.0 or higher)
  • MongoDB (v4.0.0 or higher)
  • npm or yarn

πŸ› οΈ Installation

  1. Clone the repository:
git clone [repository-url]
cd BlogForge
  1. Install dependencies:
npm install
  1. Create a .env file in the root directory with the following variables:
NODE_ENV=development
PORT=3000
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=7d
MONGODB_URI=mongodb://localhost:27017/blogforge
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
LOG_LEVEL=error
  1. Seed the database (optional):
npm run seed
  1. Start the server:
# Development mode
npm run dev

# Production mode
npm start

πŸ”‘ Authentication API

Register User

POST /api/auth/register
{
  "username": "johndoe",
  "email": "john@example.com",
  "password": "password123"
}

Login

POST /api/auth/login
{
  "email": "john@example.com",
  "password": "password123"
}

πŸ“ Posts API

Get All Posts

GET /api/posts

Create Post

POST /api/posts
Authorization: Bearer {token}
{
  "title": "My First Post",
  "content": "This is the content of my post",
  "tags": ["tech", "coding"],
  "visibility": "public"
}

Get Single Post

GET /api/posts/:id

Update Post

PUT /api/posts/:id
Authorization: Bearer {token}
{
  "title": "Updated Title",
  "content": "Updated content"
}

Delete Post

DELETE /api/posts/:id
Authorization: Bearer {token}

Like/Unlike Post

POST /api/posts/:id/like
Authorization: Bearer {token}

Search Posts

GET /api/posts/search?q=keyword&tags=tech,coding&author=johndoe&sort=recent

πŸ’¬ Comments API

Add Comment

POST /api/comments
Authorization: Bearer {token}
{
  "content": "This is a comment",
  "postId": "post_id_here"
}

Get Post Comments

GET /api/comments/posts/:id/comments

πŸ‘₯ Users API

Get All Users

GET /api/users

Get User Profile

GET /api/users/:id

Update Profile

PATCH /api/users/profile
Authorization: Bearer {token}
{
  "bio": "Software Developer",
  "location": "New York",
  "website": "https://example.com",
  "interests": ["coding", "technology"]
}

Search Users

GET /api/users/search?q=john&interests=tech&location=NewYork

Follow User

POST /api/users/:id/follow
Authorization: Bearer {token}

Unfollow User

POST /api/users/:id/unfollow
Authorization: Bearer {token}

Get Followers

GET /api/users/:id/followers

Get Following

GET /api/users/:id/following

Insomnia REST

Import the file :

Insomnia_rest_setting.json

πŸ”’ Security Features

  • JWT-based authentication
  • Password hashing using bcrypt
  • Rate limiting for API endpoints
  • XSS protection
  • NoSQL injection prevention
  • Security headers using Helmet

πŸ§ͺ Testing

Run the test suite:

npm test

Run tests with coverage:

npm run test:coverage

πŸ“ API Response Format

Success Response

{
  "status": "success",
  "data": {
    // Response data here
  }
}

Error Response

{
  "status": "error",
  "message": "Error message here"
}

βš™οΈ Environment Variables

Variable Description Default
NODE_ENV Environment mode development
PORT Server port 3000
MONGODB_URI MongoDB connection string -
JWT_SECRET JWT secret key -
JWT_EXPIRES_IN JWT expiration time 7d
LOG_LEVEL Winston logger level info

πŸ“¦ Project Structure

.
β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ authController.js
β”‚   β”œβ”€β”€ commentController.js
β”‚   β”œβ”€β”€ followController.js
β”‚   β”œβ”€β”€ postController.js
β”‚   └── userController.js
β”œβ”€β”€ middleware/
β”‚   β”œβ”€β”€ auth.js
β”‚   β”œβ”€β”€ errorHandler.js
β”‚   β”œβ”€β”€ rateLimit.js
β”‚   └── security.js
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ Comment.js
β”‚   β”œβ”€β”€ Post.js
β”‚   └── User.js
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ auth.js
β”‚   β”œβ”€β”€ comments.js
β”‚   β”œβ”€β”€ posts.js
β”‚   └── users.js
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ auth.js
β”‚   └── logger.js
└── server.js

πŸ“œ License

MIT License

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

About

A RESTful API for Modern Blogging Platforms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published