This repository contains the backend code for a web application built using Node.js, Express, and Mongoose. The backend provides API endpoints to manage User, Post, Comment, and Like functionalities.
Before running the backend, make sure you have the following installed on your system:
- Node.js (https://nodejs.org)
- MongoDB (https://www.mongodb.com)
- Clone the repository to your local machine:
git clone https://github.com/Ifeanyi-Ani/blog-backend.git
cd blog-backend
- Install the dependencies:
npm install
- Set up the environment variables:
Create a .env
file in the root directory and provide the required environment
variables. For example:
PORT=4000
DATABASE=mongodb://127.0.0.1:27017/tumblr-app
- blog-backend/
- controllers/
- authController.js
- userController.js
- postController.js
- commentController.js
- likeController.js
- errController.js
- models/
- User.js
- Post.js
- Comment.js
- routes/
- auth.js
- users.js
- post.js
- comments.js
- likes.js
- utils/
- appErr.js
- catchAsync.js
- public/
- img/
- users/
- posts/
- app.js
- server.js
-
controllers/: Contains the route handlers for each API endpoint. The controllers interact with the models to perform CRUD operations.
-
models/: Defines the Mongoose schemas for User, Post, Comment, and any other data models used in the application.
-
routes/: Contains the route definitions for each API endpoint. These route files use the corresponding controller functions.
-
public/: Contains image storage
-
utils/: Handles the middleware for catching errors and more.
-
app.js: Handles the main application logic, including setting up middleware and registering routes.
-
server.js: Handles the server logic, such as starting the server and listening for incoming requests.
To start the server, run the following command:
npm start
The server will start running on the specified port (default is 4000). You can then use tools like Postman or frontend applications to interact with the API endpoints.
The backend provides the following API endpoints:
-
User Routes and auth Routes:
GET /users
: Get all usersGET /users/:id
: Get a specific user by IDPOST /auth/signup
: Create a new userPOST /auth/login
: login a userPATCH /users/:id
: Update a userDELETE /users/:id
: Delete a user
-
Post Routes:
GET /posts
: Get all postsGET /posts/:id
: Get a specific post by IDPOST /posts
: Create a new postPATCH /posts/:id
: Update a postDELETE /posts/:id
: Delete a post
-
Comment Routes:
GET posts/:postId/comments
: Get all commentsGET posts/:postId/comments/:commentId
: Get a specific comment by IDPOST posts/:postId/comments
: Create a new commentPATCH posts/:postId/comments/:commentId
: Update a commentDELETE posts/:postId/comments/:commentId
: Delete a comment
-
Like Routes:
POST /likes/:postId
: Like a postDELETE /likes/:postId
: Unlike a post
1 Token Cookie Not Stored Automatically: The token sent as a cookie is not being automatically stored in the browser cookie. This issue is currently being investigated, and a solution will be implemented soon.
2 Incomplete Logout Logic: The logout logic is not yet complete. While users can log out, certain edge cases or cleanup processes may not be handled correctly. This functionality will be enhanced in upcoming updates.
3 Incomplete Security Measures: Some security measures, such as input validation, rate limiting, and Sanitizer, have not been fully implemented. Security is a top priority, and these measures will be added to the application to ensure it is robust and safe.
4 View Improvements: Currently, the responses from the API endpoints are basic and lack a user-friendly interface. Creating a nicer view for the responses is planned to improve the overall user experience.
Feel free to contribute to this project by opening issues or submitting pull requests. Your contributions are appreciated!
This project is licensed under the ISC License.