Task was to build an API for a social network web application where users can share their thoughts, react to friends’ thoughts, and create a friend list. The required stack includes Express.js for routing, a MongoDB database, and the Mongoose ODM. In addition to using the Express.js and Mongoose packages, I have also used a native JavaScript Date
object and Web API to format timestamps.
I have created a walkthrough video that demonstrates the functionality of the API using Insomnia after creating the API. A link to the video has been provided in the README below.
- Description
- Installation
- Usage
- Directory Structure
- Technologies Used
- Setup
- Demo
- Acknowledgements
- Questions
- License
Install dependencies using:
npm install
Need to have MongoDB and MongoDB Compass installed locally to run the app.
The application will then be invoked by using the following command:
npm run watch
.
├── config/
├── controllers/
├── models/
│ ├── Thought.js
│ ├── User.js
│ └── index.js
├── routes/
│ ├── api/
| | ├── thoughtRoutes.js
| | ├── userRoutes.js
| | └── index.js
│ └── index.js
├── utils/
├── .gitignore
├── README.md
├── LICENSE
├── package.json
├── README.md
└── index.js
- Node.js
- Express.js
- MongoDB
- Mongoose (ODM)
- Nodejs must be installed
- MongoDB Compass must be installed
- Insomnia was used to test routes
- Text editor (VS Code recommended)
This demos a general overview of the functionality of the api, and starts by demonstrating on how to invoke the application. I then explain how Mongoose syncs to MongoDB on server start, and go over the User and Thought models and their associated schemas and subdocuments. I also provide an brief over the getter method used to format dates, virtuals used, referenced models, self-references, and the objectId method to create a unique id value with a desired name:
This demos POST User, POST Thought, POST friend, POST Reaction, GET Single User, GET Single Thought, Get All Thoughts, GET All Users. This also showcases a match validation for an incorrect email and formatted dates:
/api/users
GET
all usersGET
a single user by its_id
and populated thought and friend dataPOST
a new user
/api/users/userId
PUT
to update a user by its_id
/api/users/:userId/friends/:friendId
POST
to add a new friend to a user's friend list
/api/thoughts
GET
to get all thoughtsGET
to get a single thought by its_id
POST
to create a new thought (This also pushes the created thought's_id
to the associated user'sthoughts
array field)
/api/thoughts/:thoughtId/reactions
POST
to create a reaction stored in a single thought'sreactions
array field
This demos PUT Thought, POST friend (second), DELETE friend:
/api/thoughts/thoughtId
PUT
to update a thought by its_id
/api/users/:userId/friends/:friendId
DELETE
to remove a friend from a user's friend list
This demos DELETE Reaction, DELETE Thought, DELETE User (and associated thoughts):
/api/thoughts/:thoughtId/reactions
DELETE
to pull and remove a reaction by the reaction'sreactionId
value
/api/thoughts/thoughtId
DELETE
to remove a thought by its_id
/api/users/userId
DELETE
to remove user by its_id
and removes a user's associated thoughts when deleted.
- https://www.npmjs.com/package/mongoose
- https://www.npmjs.com/package/express
- https://www.npmjs.com/package/nodemon
Created by: @moedaaboul
Feel free to contact me via [email protected]!
This work is licensed under MIT.