DEV Community

Cover image for REST API
daniel knowles
daniel knowles

Posted on

REST API

Most of the internet is based on the transference of data between the client and the server...

Requests

First the client makes a request to the server.
Usually this is done with a HTTP request, but HTTP is not the only language the server can speak. They can also speak FTP. So make sure you are using the right language.
There is also HTTPS. The 's' stands for secure. HTTPS uses cryptography to encrypt your request. This we if they are intercepted they wont be understood.
If the request to the server is valid then they will get a response in the form of data. If the request is not valid then they will receive an error message such as 404. In order for for the server to give back reply it may have to work out the results first or it may need to communicate with the database. Or it may do both. So the client needs to go through the server if they want something from the database.

Apis

'An API is an interface through which one program or web site talks to another.
They are used to share data and services, and they come in many different
formats and types.' - google
The server will have apis that are services that it can use for the client to get information from. The apis determine what is available to the client.

REST is a architectural style for designing apis. It was created by Roy Fielding as part of his research for his phd.
REST gives developers a set of rules they can follow when building their apis.

Make an api restful

there are lots of rules to follow to make an api restful. The two most important ones are using http request verbs, and using specific patterns of routes/Endpoint URLs.

HTTP verbs to follow

GET
GET is like read from CRUD, everytime we want our server to serve up some data, we can use GET. GET lates a callback that responds to the request and sends a result back.

app.get('/tapes', (req, res) => {
Tape.find()
.then(data => res.send(data))
.catch(err => console.err(err));
});

POST
corresponds to create in crud. Whenever you create a form on the website you use POST. When data is posted to the server you then create an entry in the database and save it for later. The request will contain the data, and the result will be either success or error.

app.post('/tapes', (req, res) =>{
Tape.insertMany(req.body)
.then(data => res.send(data))
.catch(err => console.log('Danger in Post', err));
});

PUT & PATCH
both go into the database and update pieces of data.

PUT will update the database by sending a entire new entry to replace the previous.

PATCH will go in and replace only the piece of data that needs to be updated instead of replacing the entire entry.

DELETE
DELETE deletes or destroys a piece of data in the database.

Patterns of routes/Endpoint URLs

In order for the api to be restful we need to have a specific pattern for endpoints and routes.
If we create a route for /tapes when a client makes a get request it should retrieve all the tapes.
If they make a post request to /tapes it should make a single new tape and add it to the database.
And when they make a delete request to /tapes it should delete all tapes.

In restful routing you have rules for individual resources so with all the tapes you have every individual tape as well, so you are able to target a specific tape in the list. If you wanted tape #3306 you could target that specific tape with app.get(/tapes/3306
you can target this specific route with all of the five HTTP verbs. Using the one that fits best for the task.

Top comments (0)