A RESTful API for a social media platform with features like user authentication, post management, comments, protected routes, and real-time notifications using Socket.io.
- Features
- Technologies Used
- Getting Started
- Project Structure
- Environment Variables
- API Endpoints
- Running Real-Time Chat
- Running the Deployed Link
- License
- User Authentication: Secure JWT-based authentication.
- Post Management: Create, view, and manage posts.
- Comments: Add comments to posts with real-time notifications.
- Protected Routes: Access restricted resources securely.
- Real-time Chat: WebSocket-powered messaging and notifications.
- Frontend Integration: Serves static frontend files.
- Backend: Node.js, Express.js
- Database: MongoDB (with Mongoose)
- Authentication: JSON Web Tokens (JWT), bcrypt
- Real-time: Socket.io
- Frontend: Static files served from
/client - Deployment: Deployed on AWS (access using the link https://www.chattydaadi.fun)
- Node.js installed on your system.
- MongoDB Atlas or a locally installed MongoDB instance.
- A
.envfile with the necessary environment variables (see Environment Variables).
- Clone the repository:
git clone https://github.com/your-username/social-media-app-api.git cd social-media-app-api - Install dependencies:
npm start
- Set up environment variables (see Environment Variables).
- Start the server
npm start
- Access the API at
http://localhost:5000
Create a .env file in the root directory and include the following:
PORT=5000
MONGO_URI=your-mongodb-uri
JWT_SECRET=your-jwt-secretReplace your-mongodb-uri and your-jwt-secret with your MongoDB connection string and a secret key for JWTs.
- POST
/api/auth/signup- Description: Register a new user.
- Body:
{ "name": "John Doe", "email": "johndoe@example.com", "password": "password123" } - POST
/api/auth/login- Description: Log in an existing user.
- Body:
{ "email": "johndoe@example.com", "password": "password123" }
- GET
/api/posts- Description: Get a list of all posts.
- POST
/api/posts- Description: Create a new post (authenticated).
- Headers:
{ "Authorization": "Bearer <token>" }- Body:
{ "text": "This is a new post", "mediaUrl": "http://example.com/image.jpg" } - POST
/api/comments- Description: Add a comment to a post (authenticated).
- Headers:
{ "Authorization": "Bearer <token>" }- Body:
{ "text": "Great post!", "postId": "<post-id>" }
- GET
/api/protected- Description: Access protected route (authenticated).
- Headers:
{ "Authorization": "Bearer <token>" }
-
Start the server
Ensure the server is running using the command:npm start
-
Connect the client(s) using the Frontend:
Use a simple HTTP server to serve theclientfolder.:npx serve
Or use the Live Server extension in VS Code.
-
Open in Browser:
- Navigate to
http://localhost:5000(or your hosting URL). - Use multiple clients (e.g., browser tabs or tools like Postman with a WebSocket add-on).
- Navigate to
-
Prerequisites:
- Browser with multiple tabs/windows.
-
Steps to Test Real-Time Chat:
- Open Multiple Clients
- Open two or more browser tabs/windows and navigate to https://www.chattydaadi.fun
- Navigate to the Chat Section.
- Send Messages:
- In one browser tab, type a message in the chat interface and send it.
- Check that the message appears instantly in all open tabs or browser windows.
- Test Notifications:
- Ensure real-time notifications are displayed when comments are made on the post (through API testing softwares like Postman).
- Simulate Multi-User Interaction:
- Open a third browser or session to simulate a new user joining the chat.
- Send messages from all active clients and verify they sync in real time.
- Open Multiple Clients
- Register a New User
- Endpoint:
POST https://www.chattydaadi.fun/api/auth/signup - Body (JSON):
{ "name": "Name Surname", "email": "name@example.com", "password": "password123" }
- Endpoint:
- Description: Registers a new user.
- Log In an Existing User
- Endpoint:
POST https://www.chattydaadi.fun/api/auth/login - Body (JSON):
{ "email": "name@example.com", "password": "password123" }
- Endpoint:
- Description: Authenticates a user and returns a token.
-
Get All Posts
- Endpoint:
GET https://www.chattydaadi.fun/api/posts - Description: Fetches all posts.
- Endpoint:
-
Create a New Post
- Endpoint:
POST https://www.chattydaadi.fun/api/posts - Headers:
{ "Authorization": "Bearer <token>" } - Body (JSON):
{ "text": "This is a new post", "mediaUrl": "http://example.com/image.jpg" }
- Endpoint:
- Description: Creates a new post (requires authentication).
- Add a Comment
- Endpoint:
POST https://www.chattydaadi.fun/api/comments - Headers:
{ "Authorization": "Bearer <token>" } - Body (JSON):
{ "text": "Great post!", "postId": "<post-id>" } - Description: Adds a comment to a post (requires authentication).
- Endpoint:
- Access Protected Data
- Endpoint:
GET https://www.chattydaadi.fun/api/protected - Headers:
{ "Authorization": "Bearer <token>" } - Description: Fetches data from a protected route.
- Endpoint:
If you encounter any error:
- Ensure your Authorization header contains a valid JWT token.
- Verify the postId or mediaUrl values exist and are properly formatted.
- Check your environment variables on the server (for debugging authentication issues).
This project is licensed under the MIT License.

