This project is a backend application for a social network platform where users can share their thoughts, react to friends' thoughts, and create a friend list. Utilizing MongoDB, an Express.js server, and the Mongoose ODM, it's designed to handle large amounts of unstructured data efficiently.
Open Demo video of Social Network API
The Social Network API is a backend application designed to power social networking features for web and mobile applications. This API provides robust support for a wide range of social networking functionalities, including:
-
User Management: Users can create accounts, update their information, and delete their accounts. Each user's profile includes a unique username, email address, and the ability to have friends and share thoughts.
-
Thoughts Sharing: Users can post thoughts, allowing them to share text-based content with others. Each thought includes the content of the thought, the creator's username, and timestamps for when the thought was created.
-
Reactions to Thoughts: Other users can react to thoughts, enabling a dynamic and interactive experience. Reactions are nested within thoughts and include a reaction body, the reacting user's username, and a timestamp.
-
Friend Lists: Users can maintain a friend list within the network. The API supports adding and removing friends, enabling users to connect and interact with others.
-
Real-time Updates: The application is designed to support real-time updates, ensuring that users can see the latest thoughts and reactions as they happen.
-
Scalable and Flexible: Built on MongoDB, a NoSQL database, the API is designed to efficiently handle large volumes of unstructured data. This makes it scalable and adaptable to the needs of growing social networks.
-
Timestamp Formatting: Utilizes customized timestamp formatting to present dates and times in a user-friendly manner, adapted to the local timezone of the user.
-
Friend Count: A virtual attribute on the User model that dynamically calculates the number of friends a user has.
-
Reaction Count: A virtual attribute on the Thought model that provides the count of reactions a thought has received.
- Cascading Deletes: When a user is deleted, all associated thoughts and reactions are automatically removed from the database, ensuring data integrity and cleanliness.
-
Ensure you have Node.js and MongoDB installed on your machine.
-
Clone the repository to your local machine:
git clone https://github.com/naturuplift/SocialNetworkAPI.git
- Navigate to the project directory and install dependencies:
cd social-network-api
npm install
- Seed the database:
npm run seed
- Start the server:
npm start
Once the server is running, you can use an API client like Insomnia to test the API routes.
- /api/users routes for users display data in a formatted JSON.
- /api/thoughts routes for thoughts display data in a formatted JSON.
- /api/thoughts/:thoughtId/reactions routes for reactions display data in a formatted JSON.
- /api/users/:userId/friends/:friendId routes for friends display data in a formatted JSON.
Below are sample screenshots showcasing the application routes functionality.
GET Find All Users:
GET Find a User (by Id):
GET Find All Thoughts:
GET Find Thoughts (by Id):
POST CREATE a New User:
POST CREATE a Thought:
POST CREATE Reaction:
POST Add Friend:
PUT Update a User (by Id):
PUT Update a Thought (by Id):
DELETE Delete a User (by Id):
DELETE Delete a Thought (by Id):
DELETE Remove Reaction (by Id):
DELETE Remove Friend (by Id):
User
- username: Unique, required, trimmed string.
- email: Unique, required string that must match a valid email address.
- thoughts: Array of
_id
values referencing theThought
model. - friends: Array of
_id
values referencing theUser
model (self-reference).
Schema Settings
- A virtual called
friendCount
retrieves the length of the user's friends array field on query.
Thought
- thoughtText: Required string between 1 and 280 characters.
- createdAt: Date with a default value to the current timestamp. A getter method formats the timestamp on query.
- username: Required string indicating the user that created the thought.
- reactions: Array of nested documents created with the
reactionSchema
.
Schema Settings
- A virtual called
reactionCount
retrieves the length of the thought's reactions array field on query.
Reaction (SCHEMA ONLY)
- reactionId: Uses Mongoose's ObjectId data type with a default value set to a new ObjectId. reactionBody: Required string with a 280 character maximum. username: Required string. createdAt: Date with a default value to the current timestamp. A getter method formats the timestamp on query.
/api/users
- GET all users
- GET a single user by its
_id
and populated thought and friend data - POST a new user
- PUT to update a user by its
_id
- DELETE to remove user by its
_id
/api/users/:userId/friends/:friendId
- POST to add a new friend to a user's friend list
- DELETE to remove a friend from a user's friend list
/api/thoughts
- GET to get all thoughts
- GET to get a single thought by its
_id
- POST to create a new thought
- PUT to update a thought by its
_id
- DELETE to remove a thought by its
_id
/api/thoughts/:thoughtId/reactions
- POST to create a reaction stored in a single thought's reactions array field
- DELETE to pull and remove a reaction by the reaction's reactionId value
Your directory may have the following structure:
SocialNetworkAPI/
├── config/
│ └── connection.js # Configures Mongoose connection to MongoDB
├── controllers/
│ ├── thoughtController.js # Contains controllers for thought operations
│ └── userController.js # Contains controllers for user operations
├── models/
│ ├── Thought.js # Defines the Thought model and schema
│ ├── User.js # Defines the User model and schema
│ └── Reaction.js # Defines the Reaction schema (used within Thought)
├── routes/
│ ├── api/
│ │ ├── thoughtRoutes.js # Routes for thought operations
│ │ ├── userRoutes.js # Routes for user operations
│ │ └── index.js # Aggregates and exports API routes
│ └── index.js # Central entry point for routing
├── seeds/
│ └── seed.js # Contains seed data for the database
├── .gitignore # Specifies intentionally untracked files to ignore
├── package.json # Defines project and its dependencies
├── README.md # Project description and guidelines
└── server.js
To contribute to this project, please create a pull request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License. See the LICENSE file for details.