Intro
REpresentational State Transfer
REST is a method of mapping between HTTP routes and CRUD
RESTful routes is a conventional pattern to follow when structuring different routes for interacting with the server whenever an HTTP request is made by the client.
RESTful Routes
There are 7 restful routes
Name | Path | HTTP Verb | Purpose | Mongoose Method |
---|---|---|---|---|
Index | /dogs | GET | List all dogs | Dog.find() |
New | /dogs/new | GET | Show new dog form | n/a |
Create | /dogs | POST | Create a new dog, then redirect somewhere | Dog.create() |
Show | /dogs/:id | GET | Show info about one specific dog | Dog.findById() |
Edit | /dogs/:id/edit | GET | Show edit form for one dog | Dog.findById() |
Update | /dogs/:id | PUT | Update particular dog, then redirect somewhere | Dog.findByIdAndUpdate() |
Destroy | /dogs/:id | DELETE | Delete a particular dog, then redirect somewhere | Dog.findByIdAndRemove() |
Why do we use RESTful Routes?
REST is merely a convention. It is not at all technically necessary when creating your application routes. However like many conventions, it is useful because it makes your application predictable, which is useful for any other developers who may be working on it in the future. Much like semantic variables.
Nested Routes
In many cases our routes will need to be nested. For example if we need comments to be associated with a specific blog post, we need to specify the ID of the blog post in the route.
NEW blogs/:id/comments/new GET
CREATE blogs/:id/comments POST
Here is a useful diagram to help understand nested Express Routes