All about REST
Tags: javascript, codenewbie
Published: 2020-10-25 14:04:29

When you are browsing a web page, have you ever wondered: where does this page get all of this information from? The short answer is: **a server**. In this article we are going to explore what an API is, more specifically a *RESTful API*, and dive a bit deeper to see how exactly a **client** communicates with a **server** to get information.

###***Okay, first thing first: the lingo***
Knowing the following terms will help you understand 
exactly what REST is and what it means to make an API RESTful.
1. Client
2. Server
3. API

####So what's a client? A server?
A client is the browser that **makes** the request for data.
This is typically done through a HTTP request: 
If you've never seen a HTTP request here is an example

```javascript
app.get((req, res){})
```

The server is, as Wikipedia puts it: 
>*a piece of computer hardware or software (computer program) that provides functionality for other programs or devices, called "clients"*

In short: servers provide the information that the client requests so as long as the information is something the server can provide.

A real world example of how the client - server relationship works is a night out at a restaurant. You, as a paying customer, are the client. You read the menu, and request the food you may like. Your waiter or waitress is the server. They listen to your request and check to see if it is something they can fulfill. If you ask for pizza at a burger place, you've made a bad request, and it won't get fulfilled.

####On to APIs!
The acronym API stands for **Application Programming Interfaces**. Now what exactly does that mean? And why are APIs useful? 
According to MDN an API  is
> a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system.

Have you ever used a weather app? More than likely they are using a weather API to update the weather conditions on their app dynamically. 

Have you used tinder? This is another great example of API usage as they use a Facebook API to show shared friends and shared interests among potential matches.

If you've ever used `JQuery`, you guessed it, you've used an API. `JQuery` gives us access to a lot of functions and objects that helps us create software. If you look back at MDN's definition of what an API is, this fits perfectly!

###**Finally! Lets talk about** *REST*
REST stands for **RE**presentational **S**tate **T**ransfer
REST is essentially just an architectural style for designing APIs. Roy fielding began REST as apart of his PHD dissertation where he developed a set of rules that web developers could follow when building their APIs. He brought about the idea that all websites should use the same structure for building their APIs. This made it much easier for clients and servers to work together and use different APIs to work quickly, efficiently, and easily.

#####so what exactly makes an API RESTful?
There are a few rules that an API has to follow in order to be considered RESTful. The two main rules are:

>1. **Must use HTTP request verbs. Those verbs include:**
    1. GET - gets the request from the server
    2. POST - posts the request to the server
    3. PUT & PATCH - updates information on the database
    4. DELETE - deletes a specific piece of information in the database
2. **Must use a specific pattern of routes/endpoint URLS**

An API must follow the following routing patterns to be considered RESTful
![RESTful routing](https://dev-to-uploads.s3.amazonaws.com/i/iipt59glzsttljuj2ew3.jpg)

#####Looking for a challenge?
After reading this article see if you can build your own RESTful API. 

#####Good Luck!