A RESTful API for a social network web application where users can share their thoughts, react to friends' thoughts, and create a friend list.
- User Management: Create, update, delete, and retrieve users
- Thought Management: Create, update, delete, and retrieve thoughts
- Friend List Management: Add and remove friends
- Reaction Management: React to thoughts
- MongoDB Database: Utilizes MongoDB for flexible data storage
- Express.js Server: Fast, unopinionated web framework
- Mongoose ODM: Elegant MongoDB object modeling
-
Clone the repository:
git clone [repository-url]
-
Install dependencies:
cd social-network-api npm install
-
Make sure MongoDB is installed and running on your system
-
Configure environment variables:
cp .env.example .env
Then edit
.env
with your specific configuration:# Required: MongoDB connection string MONGODB_URI=mongodb://127.0.0.1:27017/your_database_name # Required: Server port number (e.g., 3000, 3001, 3002) PORT=your_preferred_port # Required: Node environment (development, production, or test) NODE_ENV=your_environment
Important: Never commit your
.env
file to version control. The.env.example
file serves as a template. -
Start the server:
npm start
For development with auto-restart:
npm run dev
Variable | Description | Required | Example |
---|---|---|---|
MONGODB_URI |
MongoDB connection string | Yes | mongodb://127.0.0.1:27017/your_database_name |
PORT |
Server port number | No | Any valid port (e.g., 3000, 3001, 3002) |
NODE_ENV |
Node environment | No | development, production, or test |
Security Note:
- Always use environment variables for sensitive information
- Never commit real credentials to version control
- Keep your
.env
file secure and local to your development environment - In production, use your hosting platform's secure environment variable system
GET /api/users
GET /api/users/:userId
POST /api/users
Body:
{
"username": "john_doe",
"email": "[email protected]"
}
PUT /api/users/:userId
Body:
{
"username": "john_updated",
"email": "[email protected]"
}
DELETE /api/users/:userId
POST /api/users/:userId/friends/:friendId
DELETE /api/users/:userId/friends/:friendId
GET /api/thoughts
GET /api/thoughts/:thoughtId
POST /api/thoughts
Body:
{
"thoughtText": "Here's a cool thought...",
"username": "john_doe"
}
PUT /api/thoughts/:thoughtId
Body:
{
"thoughtText": "Updated thought content"
}
DELETE /api/thoughts/:thoughtId
POST /api/thoughts/:thoughtId/reactions
Body:
{
"reactionBody": "Love this!",
"username": "jane_doe"
}
DELETE /api/thoughts/:thoughtId/reactions/:reactionId
username
: String (Unique, Required)email
: String (Unique, Required, Valid email format)thoughts
: Array of _id values referencing Thought modelfriends
: Array of _id values referencing User model- Virtual:
friendCount
- retrieves length of user's friends array
thoughtText
: String (Required, 1-280 characters)createdAt
: Date (Default: current timestamp)username
: String (Required)reactions
: Array of nested Reaction documents- Virtual:
reactionCount
- retrieves length of thought's reactions array
reactionId
: ObjectId (Default: new ObjectId)reactionBody
: String (Required, 280 character maximum)username
: String (Required)createdAt
: Date (Default: current timestamp)
- Express.js
- MongoDB
- Mongoose ODM
- Node.js
- Moment.js (for timestamp formatting)
Use the included test script to run automated tests:
./test.sh
The test script will:
- Create test users
- Create thoughts
- Add reactions
- Test friend functionality
- Verify all CRUD operations
-
Setup Insomnia
- Download and install Insomnia
- Create a new Collection named "Social Network API"
-
Environment Setup in Insomnia
- Create a new Environment
- Add base URL variable:
{ "baseUrl": "http://localhost:your_port" }
-
Testing User Routes
a. Create a User
- Method: POST
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users
- Body (JSON):
{ "username": "testuser", "email": "[email protected]" }
b. Get All Users
- Method: GET
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users
c. Get User by ID
- Method: GET
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId
- Replace :userId with the ID from create user response
d. Update User
- Method: PUT
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId
- Body (JSON):
{ "username": "updateduser", "email": "[email protected]" }
e. Delete User
- Method: DELETE
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId
-
Testing Friend Routes
a. Add Friend
- Method: POST
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId/friends/:friendId
b. Remove Friend
- Method: DELETE
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId/friends/:friendId
-
Testing Thought Routes
a. Create Thought
- Method: POST
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts
- Body (JSON):
{ "thoughtText": "Here's a test thought", "username": "testuser" }
b. Get All Thoughts
- Method: GET
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts
c. Get Thought by ID
- Method: GET
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId
d. Update Thought
- Method: PUT
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId
- Body (JSON):
{ "thoughtText": "Updated thought content" }
e. Delete Thought
- Method: DELETE
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId
-
Testing Reaction Routes
a. Add Reaction
- Method: POST
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId/reactions
- Body (JSON):
{ "reactionBody": "This is a reaction", "username": "testuser" }
b. Remove Reaction
- Method: DELETE
- URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId/reactions/:reactionId
- Save the user IDs and thought IDs from creation responses to use in other requests
- Test error cases by providing invalid data or IDs
- Verify that virtual fields (friendCount, reactionCount) are being populated
- Check that related data is being properly populated in GET responses
- Verify cascade deletion (e.g., deleting a user should delete their thoughts)
This project is MIT licensed.
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Open a pull request