in

RESTful Routing

RESTful Routes Logo

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

A table of all 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

Frosty CMS Logo

Frosty CMS: Adding Posts To Database With Form

Frosty CMS Logo

Frosty CMS: Creating Single Blog Post Page