A back-end RESTful API for a blogging platform built with Node.js, Express, and MongoDB. Supports user authentication, blog post management and comments.
- User Management
Register/login with JWT authentication. Password hashing viabcryptjs. - 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.
| Category | Technologies |
|---|---|
| Backend | Node.js, Express |
| Database | MongoDB (Mongoose) |
| Auth | JWT, bcryptjs |
| Environment | dotenv |
- Node.js (v14+)
- MongoDB (local or cloud instance)
-
Clone the repository:
git clone https://github.com/ali-amir-code/t12-blog-api.git cd t7-blog-api -
Install dependencies:
npm install
-
Configure environment: Create
.envfile with:PORT=5000 MONGO_URI=mongodb://localhost:27017/blogging-platform JWT_SECRET=your_strong_secret_here
-
Start the server:
node server.js
API runs at
http://localhost:5000
| Method | Endpoint | Description | Auth |
|---|---|---|---|
| POST | /register |
Register new user | No |
| POST | /login |
Authenticate user | No |
| 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 |
| 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.
curl -X POST http://localhost:5000/api/users/register \
-H "Content-Type: application/json" \
-d '{
"username": "aliamir",
"email": "ali@example.com",
"password": "password123"
}'curl -X POST http://localhost:5000/api/users/login \
-H "Content-Type: application/json" \
-d '{
"email": "ali@example.com",
"password": "password123"
}'curl -X POST http://localhost:5000/api/posts \
-H "Authorization: Bearer <your_jwt_token>" \
-F "title=Post Title" \
-F "content=Post content" \