RESTful Routing 101: A simple guide to building better sites

jackLyons Web Development Tags: ,
0

In this post I want to walkthrough the basic principles of RESTful routing. This is an absolutely essential concept to understand as it allows you to build cleaner websites as well as work with other websites to access their API’s.

restful-routing-tutorial

Why RESTful routing?

Think of it as a basic set of “principles” upon which all websites and their API’s are built upon. By building a website using RESTful principles you essentially provide the blueprint to your site, thus making it possible for other developers to utilise your code in their applications and visa versa. Seeing as the majority of the web is now open source it is absolutely essential to build your pages and web app’s with the RESTful principles in mind.

What does it mean to be RESTful?

I’m no expert on the subject. But from what I know REST stands for “REpresentational State Transfer”. It’s a specific approach to designing websites that allows sites to communicate with each other and is often used in app’s. Think of Facebook or Twitter, for example. Whenever you see a customized twitter feed on someone’s blog then you’ll be sure to know that it was created by accessing twitter’s API, which of course, was built using RESTful principles.

But in order to be RESTful, a website or application must adhere to a basic set of operations or verbs as they are commonly known. There are FOUR different RESTFUL verbs: GET, POST, PUT and DELETE. These verbs co-exist with their own web page. Take the HOME page of a website for example. The verb to GET is active here. When you load the HOME page you are literally getting the data from the server for that page.

Pop Quiz: What verb would you use when writing a comment under a photo in Facebook?

My RESTful Blog

Below I have built a table and colour coded each verb, along with the specific page that you would normally see each verb in action. I have used a simple blog to illustrate my point.

So, take my domain “www.jacklyons.com” for example. The “/blogs” page would immediately act as the home page where all my blogs are displayed. I could then click on a NEW POST button on my blog to create a new post. When I am finished I would go ahead and click PUBLISH POST. This would CREATE a new post back on the “/blogs” homepage.

REST-TABLE
My blog’s RESTful routes.

If I wanted to look at a specific blog post and read the rest of the content, I could go ahead and click the READ MORE button and then I would be directed to the “:id” of that post on a new page with that particular id. If I noticed a spelling mistake on my new post I could easily click the UPDATE button and be directed to the edit page at that particular blog id: “blogs:id/edit”.

And finally, if I absolutely hated my post and never wanted anyone to ever read it I would then simply go ahead and click DELETE POST.

As you can see all these verbs make a lot of sense: GET to get content, POST to post content, CREATE to create content, SHOW to show a new post, UPDATE to edit a post and DESTROY to delete a post.

Databases and CRUD

As you can see, in the table there is a set of Mongoose methods to go along with each verb. Don’t worry if you are unfamiliar with MongoDB, it’s just that I have been learning the MEAN stack (MongoDB, Express, Angular and Node) and that’s why I am familiar with MongoDB. Basically all these methods mean is that when you go to get a page or a piece of information for that matter, you simply use a method, in this case “Blog.find()”. The rest is pretty self explanatory.

However, what you might here often association with REST is the acronym CRUD, which stands for: Create, Retrieve, Update, Delete. These are the basic actions that are involved on the database side of the equation. Here’s a simple explanation of CRUD:

“When you have a database and you input an entry into the table, you are accessing the (C)reate command. If you pull up a saved table and read the details, you are (R)etrieving the data. If you go a step further and then change the details within a field of the table, you are using the (U)date command. When a field within the table is no longer needed, you can prompt the program to (D)elete that field.

Source: Software Career Today

So there you have it. Now you know what it means to be RESTful. Next time you go to access an API at least you will have a bit more of an idea of where to look and how it works.

If you found this post helpful and would like me to follow up with a post on how to use an API, or any other topic for that matter, simply leave a comment below.

Jack.


The Web Developers Bootcamp.

A n00b friendly guide to becoming a pro!

Learning to code can be seriously hard work. That’s why I’ve started to substitute my education with an incredible online resource called the Web Developers Bootcamp courtesy of Udemy. This course has helped me brush off the cobwebs, unlearn my self-taught bad habits and gain a solid understanding of the true fundamentals of web development. It has helped me see why it is important to understand these JavaScript problems and how they apply to the real world.

This bootcamp will teach you how built beautiful sites with Bootstrap, add in functionality with JavaScript and write back end code with Node.js all in a series of short, sharp, no BS video modules. I seriously could not recommend it any higher. Check it out and let me know what you think 🙂

The Web Developers Bootcamp
The Web Developer Bootcamp.
The only course you need to learn web development – HTML, CSS, JS, Node, and More!

Sign up to receive up-to-date web tips, tricks and code hacks!