Series: Introduction to the MEAN Stack
Today you will find a major theoretical article about REST and why this concept is so important. This article will be the basis for our later server API.
REST or a RESTful API is a concept to exchange and manipulate data between the database via a server with a client using HTTP requests. The abbreviation REST stands for Representational State Transfer. For this purpose, the method names of HTTP are called a CRUDFunctionality. This is for Create, Read, Update, and Delete, and represents the basic operations that you can apply to data. The required data is described through a URL, each containing a URL for a collection of data and a URL for a single record from this collection. (Note: Even if I use terms from the NoSQL world to explain the REST concept, REST can also be used with classic SQL databases.) However, not all CRUD commands can be applied to these URLs. This results in the following concept:
HTTP method | URL Collection: /{collection} |
URL Document: /{collection}/{id} |
---|---|---|
POST (Create) | Creates a new document for this collection. | – |
GET (Read) | Returns multiple documents from the collection. | Returns a single document. |
PUT (Update) | – | Updated a single document. |
DELETE (Remove) | Deletes multiple documents in the collection. | Deletes a single document. |
This concept can also be used to map hierarchies. Suppose we have a collection for blog articles and for readers with a 1-to-n relationship (an article has any number of comments). So you could address with the URL the second comment of the first article. More fine filters could be set via query parameters. If you want to address all blog article by a certain authors from 2010 to 2012, the corresponding URL could potentially look like this: . This is not necessarily the URL that your reader’s readers see and use, but the URL that the client uses to get the corresponding record./articles/1/comments/2
/articles?author=Pipo&from=2010&to=2012
This is a very brief explanation for the REST concept, which should be sufficient for a basic understanding. The idea of REST was formulated by Roy Fielding in his dissertation 2000.
Now one could ask the question what we need REST at all? Finally, our previous examples have already shown how to send data from the database via the server to the client. There are many reasons for REST: REST allows not only a simple query of the data from the database to the client, but also the manipulation of this data – the way in the other direction. Since REST is based on HTTP, it is also universally applicable. Assuming next to a webapplication, you also want to develop a native app for iOS or Android. With REST, you can use the same server and the same database for both the Web and native app. You do not have to make any code changes on the server side for different clients. As a further reason, REST allows for the simple downsizingload data and thus much more dynamic surfaces (keyword: AJAX). In the end, REST is an easy-to-understand interface. The limitation to CRUD commands limits the scope of the API when creating the API, but on the other hand, the API remains as clear and similar to other platform APIs. Once you have used a REST API, you will quickly find your way to other REST APIs.
According to this theoretical article, we will use the REST concept next time!
0 Comments