Day 93: 4 December 2020
I continue to study backend programming to alleviate my Imposter Syndrome.
I already completed the first part and today's goal was to complete the course on freeCodeCamp regarding APIs and Microservices.
- Basic Node and Express
- MongoDB and Mongoose
- Introduction to the MongoDB and Mongoose Challenges
- Install and Set Up Mongoose
- Create a Model
- Create and Save a Record of a Model
- Create Many Records with model.create()
- Use model.find() to Search Your Database
- Use model.findOne() to Return a Single Matching Document from Your Database
- Use model.findById() to Search Your Database By _id
- Perform Classic Updates by Running Find, Edit, then Save
- Perform New Updates on a Document Using model.findOneAndUpdate()
- Delete One Document Using model.findByIdAndRemove
- Delete Many Documents with model.remove()
- Chain Search Query Helpers to Narrow Search Results
I completed the part about Express and I wrote the last three middlewares.
The first middleware shows the current date when visiting http://www.mywebsite.com/now
The second middleware shows how to display the parameter sent from the address bar http://www.mywebsite.com/word-that-will-be-displayed/echo
The third middleware shows how to get data from a POST request and the route method.
I usually put every function in a separate folder called "controller" and then I export them. I create a router that links the controller function to each route and doing so I can separate the routing logic and the inner working of each function.
The first part talked about Node.js and Express, while the second part introduces databases using MongoDB.
MongoDB is a NoSQL database and it uses JSON instead of SQL tables to store the information.
I prefer to dedicate a single file for every Schema and export them.
Once we have our model in place we can write the code for common CRUD operations. For example we can:
- create a person and store the informations in our database
- create multiple persons starting from an array of people
- find a person by name
- find a person who likes a specific food
- find a person by ID (unique number assigned to each record in our database)
- find a person by ID and add a dish to their list of favorite foods
- change the age of a person
- delete a person from our database by ID or name
- and chain several filters
I always used the then & catch methods to return a Promise and send a response status and a JSON message, but now I discovered this alternative of passing a callback!
Having followed the "same" course on OpenClassRooms and on freeCodeCamp I appreciate both methods.
On freeCodeCamp the learning approach is progressive and simpler overall.
While on OpenClassRooms at some point the course gives for granted certain concepts, but also, it suggests a structure for our project that looks way more complex and professional to me.
**Link to tweet:**