A RESTful API for a blogging platform built with Node.js, Express, PostgreSQL, and Sequelize. This API allows users to manage posts and comments, with support for user authentication and pagination.
- User authentication with JWT
- JWT secured API's
- CRUD operations for posts and comments
- Searching on posts and comments
- Pagination for fetching posts
-
Pre-requisite
- Node JS
- PostgreSQL
-
Clone the Repository
git clone https://github.com/syedahsan297engr/blog-api.git cd blog-api/backend
-
Install Dependencies
npm install
-
Create a
.env
file: Add the following environment variables:JWT_SECRET=your_jwt_secret PORT=your_port_number
-
Create a database: Set up a PostgreSQL database.
-
Update
db.config.js
: Edit the file located atbackend/config/db.config.js
to include your database configuration:module.exports = { development: { username: 'your_db_username', password: 'your_db_password', database: 'your_db_name', host: 'your_db_host', dialect: 'postgres', }, // other environments... };
This guide provides instructions on how to manage database migrations using Sequelize.
-
Ensure Database Configuration
Make sure your database configuration is correctly set in the
db.config.js
file located atbackend/config/db.config.js
. This file should include your database connection details. -
Run Command For Migration
npm run db:migrate
Start the project in development mode using nodemon
:
npm run dev
Start the project in production mode
npm start
This section provides an overview of the API endpoints available in the Backend Project. Use this guide to understand the available endpoints and how to interact with them using Postman or other tools.
All endpoints use the base URL: http://localhost:3000/
All endpoints require authentication using a Bearer token. Include the token in the Authorization
header of your requests.
issued upon sign in / sign up. Place that bearer token in authorization section.
- Method: GET
- URL: /
- Description: Checks if the server is up and running.
- Method: POST
- URL: /auth/signup
- Body:
{ "user_name": "your_user_name", "email": "your_email", "password": "password" }
- Description: Registers a new user.
-
Method: POST
-
URL: /auth/signin
-
Body:
{ "email": "your_email", "password": "password" }
-
Description: Logs in a user and provides a Bearer token.
-
Method: POST
-
URL: /posts
-
Body:
{ "title": "FrontEnd Development", "content": "ReactJs HTML CSS JS Tailwind" }
-
Description: Creates a new post.
- Method: GET
- URL: /posts
- Description: Retrieves all posts.
- Method: GET
- URL: /posts/
- URL Params:
post_id
: ID of the post to retrieve.
- Description: Retrieves a specific post by ID.
-
Method: PUT
-
URL: /posts/
-
URL Params:
post_id
: ID of the post to update.
-
Body:
{ "title": "Updated title", "content": "Updated content" }
-
Description: Updates a specific post.
- Method: DELETE
- URL: /posts/
- URL Params:
post_id
: ID of the post to delete.
- Description: Deletes a specific post.
-
Method: POST
-
URL: /comments
-
Body:
{ "post_id": 5, "title": "Deeper Nested Reply", "content": "This is another level of nesting, reply to the fourth comment.", "parent_comment_id": 7 }
-
Description: Creates a new comment on a post.
- Method: GET
- URL: /comments/
- URL Params:
comment_id
: ID of the comment to retrieve.
- Description: Retrieves a specific comment by ID.
- Method: GET
- URL: /comments/post/
- URL Params:
post_id
: ID of the post to get comments for.
- Query Params: page: Page number for pagination. limit: Number of comments to return.
- Description: Retrieves all comments for a specific post.
- Method: PUT
- URL: /comments/
- URL Params:
comment_id:
ID of the comment to update.
- Body:
{ "content": "But I don't enjoyed it" }
- Description: Registers a new user.
- Method: GET
- URL: /posts-comments
- Description: Retrieves all posts along with their comments.
- Method: GET
- URL: /posts-comments/user/
- URL Params:
user_id
: ID of the user to get posts and comments for.
- Description: Retrieves all posts and comments for a specific user.
- Method: GET
- URL: /comments
- Query Params:
content
: Content to search for in comments.
- Description: Searches for comments based on content.
- Method: GET
- URL: /posts-comments/search
- Query Params:
title
: Title to search for in posts.limit
: Number of posts to return.
- Description: Searches for posts based on title.
Thank you for exploring the Blog API! This documentation aims to provide a comprehensive overview of how to set up, configure, and interact with the API. Here are a few final notes:
-
Security: Ensure you use a strong, unique JWT secret in your
.env
file. For production, consider additional security measures such as HTTPS and environment-based configurations. -
Testing: Use tools like Postman or Insomnia for testing the API endpoints. Consider writing automated tests to ensure the stability of your API.
-
Contributing: Contributions to improve the API are welcome! Please follow the guidelines in the repository for submitting pull requests and reporting issues.
-
Feedback: Your feedback is valuable! If you encounter any issues or have suggestions for improvements, please reach out via the repository issues page.
-
Future Enhancements: Planned enhancements may include features such as user roles and permissions, richer search functionalities, and integration with external services. Stay tuned for updates!
Feel free to reach out if you have any questions or need further assistance.