![]() |
VOOZH | about |
The Richardson Maturity Model (RMM), proposed by Leonard Richardson, is a model used to assess the maturity of a RESTful API based on its implementation levels. It consists of four levels, each representing a stage of maturity in the design and implementation of RESTful principles. Let's delve into each level to understand how APIs progress through the model. In determining the maturity of a service, Richardson emphasized three main factors. They include:
Table of Content
A Uniform Resource Identifier (URI) is a unique sequence of characters used by web technologies to identify resources on the web.
Hypertext Transfer Protocol (HTTP) is a protocol used to transfer hypermedia documents. HTTP requests are sent to servers by HTTP clients in the form of request messages. HTTP defines a set of request methods to specify the action to be taken on a given resource.
HATEOAS (Hypermedia as the Engine of Application State ) refers to discoverability. The client can interact with a REST API solely through the serverβs responses. It is a self-documentary Hypermedia. Clients need not refer to any documentation to interact with a new API.
REST services are divided into maturity levels according to the Richardson Maturity Model.
POST /api) for all operations./users, /products).POST /users to create, GET /users/:id to read).Content-Type, Accept) for negotiation and metadata.We will create a RESTFUL API called gfg-wiki. We will insert articles and send HTTP requests. In this process, we will fetch, modify and delete articles. Robo3T will be used for the database. Postman will be used to send requests. In order to create a RESTFUL API in node.js, install :
Create a new directory, go to the terminal, and initialize NPM by running the following command .
npm init -y
npm i body-parser mongoose express The updated dependencies in package.json file will look like:
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.19.2",
"mongoose": "^8.4.1",
}
Example: Implementation to show the above model with an example.
Consider a database of articles with a title and content.
{ "title" : "gfg", "content" : "GeeksforGeeks is a computer science portal for geeks. " } { "title" : "REST", "content" : "REST stands for REpresentational State Transfer. " { "title" : "API", "content" : "Application Programming Interface" } { "title" : "richardson-model", "content" : " Grades APIs based on their adherence to REST constraints" } { "title" : "Http", "content" : "Hypertext Transfer Protocol (HTTP) is a protocol used to transfer hypermedia documents. " }
As you can see, the database looks like this:
The following shows how to create a database and insert documents.
Set up MongoDB and write the schema for our articles to create models. To set up MongoDB, we will use mongoose. We will connect our application to the MongoDB location and add the database name to the URL string. By default, MongoDB uses port 27017.
mongoose.connect("mongodb://localhost:27017/gfg-wiki", {useNewUrlParser: true});Schema defines the structure of our collection. We will create a schema named articleSchema consisting of two fields - title and content of the article
const articleSchema = {
title: String,
content: String
};
Now we will create a model from the articleSchema
const Article = mongoose.model("Article", articleSchema);Add the following code to your existing code in the app.js file.
Accessing all the articles using the GET method. We can fetch all the articles by sending a get request by specifying the route of the resource and a callback function that handles the request.
app.get(route, (req,res)=>{
})
To retrieve all the articles, we have to find the articles and read them from the database.
<ModelName>.find({conditions},function(err,results){
//using the result
});
Add the following code to your existing app.js file.
Start your application by running the following command
node app.jsOutput: We can access the articles at localhost:3000/articles.
Create a new article using the POST method. We will create a new article that will be added to the database. Here, the client sends data to the server.
We don't have a front-end yet, but we do have a server that has access to our database. We will test our API using Postman, rather than creating a form or a front end. Our goal is to send a post request to our server.
We will use the post method:
app.post(route,(req,res)=>{
...
})
Once the client sends the post request we need to grab that data by req.body.
Head over to postman and send a post request to localhost:3000/articles. Under the body tab, change the encoding to form-url coding, and add the title and content in key, along with the value that represents the data that we want to send along with the request.
| Key | Value |
| title | http verbs |
| content | The most common HTTP verbs are POST, GET, PUT, PATCH, and DELETE. |
We need to save this article in our database.
const <constantName>=new <ModelName>({
<fieldName>:<fielddata>,..
});
Add the following code to the previous code in the app.js file
Restart your server and send a post request using postman.
Output: Go to Robo3T and refresh your collection to view the added article. Now we have an extra entry.
We will read a specific article from our database using the findOne method.
<ModelName>.findone({conditions},(req,res)=>{
});
here, we will fetch the article with a title REST
Add the following code to your app.js file.
Output: We will specify the article title in the URL and the article whose title matches that will be displayed.
We want to submit a new version of an article. To replace an existing article, we will send a put request.
app.put(route ,(req,res)=>{
...
});
We will update the article using the Mongoose update method.
The overwrite specifies that we want to replace the entire article.
<ModelName>.update(
{conditions},
{updates},
{overwrite:true}
(err,results)=>{
})
Add the following code to your app.js file
In this case, we will change our title from API to Postman, and its content from Application Programming Interface to Postman is an API platform.
title: Postman
content: Postman is an API platform
By sending a put request to the route localhost:3000/articles/API If the server finds a parameter with a title of API, it will replace the title with a new title and the content with a new one.
We will update an existing article by sending a Patch request with the title of the article we wish to update. To update the article, we must give the fields we want to change in the body tab.
Now that we are changing only one field of an article rather than the entire article, the overwrite method is not needed when we call the update method to update our database. To update the article, we must give the fields we want to change in the body tab.
Add the following code in your app.js file to modify the article.
Output: It Updates only the fields that we provide. The title of the article REST is updated to Restful.
To delete all the articles from our database we will use deleteMany mongoose method and send a delete request from the postman.
Add the following code to your app.js file
Output: We will send a delete request to localhost:3000/articles to remove all our articles. Visit Robo3T and refresh your collection. If we send a delete request from our postman, we will not observe any articles.
The final app.js file: