An example about how to create a RESTFUL API using Express.js framework. All CRUD operations are provided: Create, Read, Update and Delete records.
Testing the requests sent in the Postman application with the 'RestClient' plugin in VS Code
Clicking on 'Send Request' in the "restful.http" file will get a response in a new tab called 'Response' after the request has been sent:
Instead of printing errors in the console, we can return them as respond:
Using the http-errors package for easier error handling. For another error, to give a meaningful error message to the user by using the error code given by mongodb for that error:
- Using the Joi package that allows us to validate easily.
- Adding our own method called joiValidation to UserSchema in our userModel.js file:
- With the toJSON() method I wrote in the userModel, the fields that I do not want the user to see in the post and get requests will not be printed on the screen:
Before -> After :
- Hashing Passwords
- I installed the "bcrypt" package so that the user passwords do not appear in the db. In this way, passwords will be kept securely. Thanks to this package, we hash passwords and save them in the database.
For Example: password123 => $2b$08$NOwiI4eDM8ZKZaFB5akH/e./.6q/jGYLM/Ys6oUp/L2MaPWkt50cG
And then in the database: