Skip to content

An example about how to create a RESTFUL API using Express.js framework. All CRUD operations are provided: Create, Read, Update and Delete records.

Notifications You must be signed in to change notification settings

devSuatt/nodejs-restful-api

Repository files navigation

nodejs-restful-api

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

resim

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:

resim


Instead of printing errors in the console, we can return them as respond:

resim

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:

error


  • Using the Joi package that allows us to validate easily.
  • Adding our own method called joiValidation to UserSchema in our userModel.js file:

validation


  • 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:

asd

Before -> After :

resim

  • 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:

resim

About

An example about how to create a RESTFUL API using Express.js framework. All CRUD operations are provided: Create, Read, Update and Delete records.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published