This repository contains the backend code for the Socio social media application. It provides endpoints for user authentication, post management, user management, and more.
- Express.js: Framework for building the RESTful API.
- MongoDB: NoSQL database used for storing user data, posts, and comments.
- Mongoose: MongoDB object modeling tool used for interacting with the database.
- Bcrypt: Library for hashing passwords.
- JSON Web Tokens (JWT): Used for user authentication and authorization.
- Nodemailer: Module for sending emails, used for features like password reset.
- Multer: Middleware for handling file uploads.
- Helmet: Middleware for securing Express apps with various HTTP headers.
- Morgan: HTTP request logger middleware.
- index.js: Entry point of the application. Sets up the Express server, connects to MongoDB, defines middleware, and specifies route handling.
- models: Directory containing Mongoose schemas for defining the structure of data stored in MongoDB.
Post.js
: Defines the schema for posts, including fields like user ID, description, image, likes, and comments.User.js
: Defines the schema for users, including fields like username, email, password, profile picture, followers, and following.
- routes: Directory containing route handlers for different endpoints.
auth.js
: Handles user registration, login, username/email availability checks, and password reset.email.js
: Provides functionality for sending emails, including a test email feature.posts.js
: Handles CRUD operations for posts, including creation, updating, deletion, liking, commenting, and retrieval.users.js
: Handles user-related operations such as updating user profiles, deleting accounts, searching for users, following/unfollowing users, and retrieving user details.
- public/images: Directory for storing uploaded images.
- assets: Directory containing helper functions for JWT token creation and email sending.
- Clone the repository.
- Install dependencies using
npm install
. - Set up a MongoDB database and provide the connection URL in a
.env
file. - Set up SMTP credentials for sending emails and provide them in the
.env
file. - Run the server using
npm start
.
/api/auth/register
: POST request to register a new user./api/auth/login
: POST request to log in an existing user./api/auth/relogin
: POST request to re-login a user./api/auth/forgot-password
: POST request to initiate the password reset process./api/posts
: POST request to create a new post./api/posts/:id
: PUT request to update a post, DELETE request to delete a post, GET request to retrieve a post by ID./api/posts/:id/like
: PUT request to like/unlike a post./api/posts/:id/comment
: PUT request to add a comment to a post, PUT request to delete a comment from a post./api/posts/timeline/:userId
: GET request to retrieve timeline posts for a user./api/posts/profile/:userId
: GET request to retrieve profile posts for a user./api/users/:id
: PUT request to update a user profile, DELETE request to delete a user account, GET request to retrieve user details./api/users/:identifier/search
: GET request to search for users by username or first name./api/users/:id/follow
: PUT request to follow a user,/api/users/:id/unfollow
: PUT request to unfollow a user.
This section outlines the structure of requests for each endpoint in the Socio API.
- Endpoint: /api/auth/register
- Method: POST
- Request Body:
{ "username": "String", "email": "String", "password": "String", "firstName": "String", "lastName": "String", "dob": "Date" }
- Response: Newly created user object
- Endpoint: /api/auth/login
- Method: POST
- Request Body:
{ "email": "String", "password": "String" }
- Response: User object if successful, error message otherwise
- Endpoint: /api/auth/relogin
- Method: POST
- Request Body:
{ "email": "String", "password": "String" }
- Response: User object if successful, error message otherwise
- Endpoint: /api/auth/forgot-password
- Method: POST
- Request Body:
{ "Email": "String" }
- Response: Status message
- Endpoint: /api/posts
- Method: POST
- Request Body:
{ "userId": "String", "desc": "String", "img": "String" }
- Response: Newly created post object
- Endpoint: /api/posts/:id
- Method: PUT
- Request Body:
{ "userId": "String", "desc": "String", "img": "String" }
- Response: Status message
- Endpoint: /api/posts/:id
- Method: DELETE
- Response: Status message
- Endpoint: /api/posts/:id
- Method: GET
- Response: Post object
- Endpoint: /api/posts/:id/like
- Method: PUT
- Request Body:
{ "userId": "String" }
- Response: Status message
- Endpoint: /api/posts/:id/comment
- Method: PUT
- Request Body:
{ "userId": "String", "comment": "String", "virtId": "String" }
- Response: Status message
- Endpoint: /api/posts/:id/del-comment
- Method: PUT
- Request Body:
{ "userId": "String", "comment": "String" }
- Response: Status message
- Endpoint: /api/posts/timeline/:userId
- Method: GET
- Response: Array of post objects
- Endpoint: /api/posts/profile/:userId
- Method: GET
- Response: Array of post objects
- Endpoint: /api/users/:id
- Method: PUT
- Request Body:
{ "userId": "String", "username": "String", "email": "String", "password": "String", "firstName": "String", "lastName": "String", "dob": "Date", "profilePicture": "String", "coverPicture": "String", "desc": "String", "state": "String", "relationship": "Number" }
- Response: Status message
- Endpoint: /api/users/:id
- Method: DELETE
- Response: Status message
- Endpoint: /api/users/:id
- Method: GET
- Response: User object
- Endpoint: /api/users/:identifier/search
- Method: GET
- Response: Array of user objects
- Endpoint: /api/users/:id/follow
- Method: PUT
- Request Body:
{ "userId": "String" }
- Response: Status message
- Endpoint: /api/users/:id/unfollow
- Method: PUT
- Request Body:
{ "userId": "String" }
- Response: Status message
- Ensure proper configuration of environment variables for database connection, SMTP server, etc., before running the server.
- Implement appropriate security measures, such as input validation, error handling, and authentication/authorization checks, to ensure the security of the application.
- This README provides an overview of the API and its functionality. For detailed usage instructions and endpoint documentation, consider generating API documentation using tools like Swagger or Postman.