Closed github-learning-lab[bot] closed 2 years ago
Good Job! 👍 You are finished! But, there is still much more you can do with Node.js, Express.js, and MongoDB. If you want to continue working on this, here are some next steps to improve security:
Encrypt your user passwords with a library like "bcrypt" before saving them.
Add a nested collection (activities, pets, blog posts, etc... ) to your User model using the "populate" method.
Use a library like JSON web token to authenticate your users in a login route.
Use middleware for your mongoose model or express routes to check if there is a valid token that matches the user's id before saving data.
At this point, your server.js file should look something like this:
This code works, but it is pretty repetitive. In general, it is a good practice to refactor code like this so it is easier to maintain.
Create function for response
We are handling the response in the same way each time. We should define a function to shorten this and make it easier to maintain. Paste this in your server file above your routes:
Now, let's call the "sendResponse" function in our mongoose callback functions:
You can always replace the generic sendResponse function with a custom callback, if you need more flexibility.
Using spread syntax (...)
We can shorten these routes further by using the JavaScript spread syntax, which copies each key/value pair in our newData object.
So, rather than this:
we can write this:
{...req.body.newData}
.Our routes should look like this now:
The code above is much easier to maintain. Also, with the spread syntax you only need to include values that are being changed. In the first version we were explicitly looking for a value at each key to update. If the values were not included, they would be set to "null". Now our routes are more flexible, since the object to update is defined by the request. This might seem weird at first, but the model should keep your data consistent.
Test this in Postman to make sure you refactored correctly. When you are finished, commit these changes and push to GitHub.