As we as developers begin to build our applications, whether they be small or large, we aim to create an experience that is user friendly and interactive. In order to not only make an application that is desirable to users, it's also beneficial to build something that when shared with other programmers has code that is easy to understand, and decipher for effective and simple collaboration. One common practice to follow when developing these apps is following the convention of RESTful routes.
What are RESTful routes?
REST stands for representational state transfer, which put simply is just a blueprint for defining the routes within our app. It provides a way to map our CRUD (create, read, update, delete) actions with our HTTP verbs (GET, POST, PUT, DELETE).
For the sake of giving you a real life example, I have created a fitness finder app that allows authenticated users to interact with gyms and their workout classes. Users can view all (GET) gyms, edit (PUT) a gym, delete (DELETE) a gym, add (POST) a gym, as well as write (POST) a review, view (GET) all reviews for specific gyms, and view (GET) all of their own gyms. All of these different features require specific routes. While building out these routes, I followed the RESTful routes conventions, or standard set of rules to complete these CRUD actions using HTTP verbs to make a request to my server.
A few examples of the REST API endpoints for that specific application are as follows:
- VIEW (GET) ALL GYMS - '/gym'
- VIEW (GET) ONE GYM - '/gym/int:gym_id'
- VIEW (GET) ALL WORKOUT CLASSES - '/workout_classes'
- EDIT (PUT) ONE WORKOUT CLASS - '/workout_classes/int:workout_class_id'
- DELETE (DELETE) A GYM - '/gym/int:gym_id'
RESTful routes are stateless, they do not hold state from one request to another. The information needed for each request is held in that specific request only. It is a stateless transaction of data.
Another benefit to using RESTful routes when building an application is the HTTP status codes. Simple, clear status codes are effective in helping developers and clients to more clearly understand where the error in the request may be occurring.
(image from https://appmaster.io/blog/the-six-rules-of-rest-apis)
RESTful routes also follow a few basic naming conventions such as, using nouns and not verbs ('/gyms' vs '/getgyms') and using plurals to show a collection instead of singular item, keeping clear definition of parent/child relationships ('/gym/int:gym_id/reviews' to access a specific gym's reviews only, and many other conventions.
While this is only scraping the surface on RESTful routes, I hope it can give you a better understanding of the benefits to using RESTful routes when building any application.
Top comments (0)