Skip to main content

RESTful Routing

Intro

REpresentational State Transfer

REST is a method of mapping between HTTP routes and CRUD

Create Read Update Delete

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

NamePathHTTP VerbPurposeMongoose Method
Index/dogsGETList all dogsDog.find()
New/dogs/newGETShow new dog formn/a
Create/dogsPOSTCreate a new dog, then redirect somewhereDog.create()
Show/dogs/:idGETShow info about one specific dogDog.findById()
Edit/dogs/:id/editGETShow edit form for one dogDog.findById()
Update/dogs/:idPUTUpdate particular dog, then redirect somewhereDog.findByIdAndUpdate()
Destroy/dogs/:idDELETEDelete a particular dog, then redirect somewhereDog.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

Comments