Boundless tutorial network : REST is an architecture based on Web standards and uses HTTP protocol. It expands around resources, in which each component is a resource, and uses HTTP standard methods to access resources through public interfaces. REST was first proposed by Roy Fielding in 2000.
HTTP method
In a REST based architecture, the following four HTTP methods are commonly used.
-
GET - used to provide read-only access to resources.
-
PUT - used to create a new resource.
-
DELETE - used to DELETE resources.
-
POST - used to update existing resources or create new resources.
Create RESTful
Consider that we have a JSON based user database in the file users JSON has the following users:
{ "user1" : { "name" : "mahesh", "password" : "password1", "profession" : "teacher", "id": 1 }, "user2" : { "name" : "suresh", "password" : "password2", "profession" : "librarian", "id": 2 }, "user3" : { "name" : "ramesh", "password" : "password3", "profession" : "clerk", "id": 3 }}
Based on this information, we will provide the following restful APIs.
number | URI | HTTP method | POST body | output |
---|---|---|---|---|
1 | listUsers | GET | empty | Displays a list of all users. |
2 | addUser | POST | JSON string | Add new user details. |
3 | deleteUser | DELETE | JSON string | Delete existing users. |
4 | :id | GET | empty | Displays the user's details. |
listUsers routing
Let's go to the server JS file, use the following code to implement our first RESTful API listUsers-
var express=require('express'); var app=express(); var fs=require("fs"); app.get('/listUsers', function (req, res) { fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { console.log( data ); res.end( data ); }); }) var server=app.listen(8081, function () { var host=server.address().address var port=server.address().port console.log("Example app listening at http://%s:%s", host, port) })
Now try using any REST client to use the URL on the local machine: http://127.0.0.1:8081/listUsers And HTTP methods: GET accesses the defined API.
{ "user1" : { "name" : "mahesh", "password" : "password1", "profession" : "teacher", "id": 1 }, "user2" : { "name" : "suresh", "password" : "password2", "profession" : "librarian", "id": 2 }, "user3" : { "name" : "ramesh", "password" : "password3", "profession" : "clerk", "id": 3 } }
addUser route
The following API will show you how to add a new user to the list. Here are the details of the new user-
user={ "user4" : { "name" : "mohit", "password" : "password4", "profession" : "teacher", "id": 4 }}
You can use Ajax calls to accept the same input in JSON, but from a pedagogical point of view, we've hard coded it here. The following is the addUser API for new users in the database-
var express=require('express'); var app=express(); var fs=require("fs"); var user={ "user4" : { "name" : "mohit", "password" : "password4", "profession" : "teacher", "id": 4 } } app.post('/addUser', function (req, res) { //First read existing users. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { data=JSON.parse( data ); data["user4"]=user["user4"]; console.log( data ); res.end( JSON.stringify(data)); }); }) var server=app.listen(8081, function () { var host=server.address().address var port=server.address().port console.log("Example app listening at http://%s:%s", host, port) })
Now try using any REST client to use the URL on the local machine: http://127.0.0.1:8081/addUser And HTTP methods: POST accesses the defined API. This should produce the following output-
{ "user1":{"name":"mahesh","password":"password1","profession":"teacher","id":1}, "user2":{"name":"suresh","password":"password2","profession":"librarian","id":2}, "user3":{"name":"ramesh","password":"password3","profession":"clerk","id":3}, "user4":{"name":"mohit","password":"password4","profession":"teacher","id":4} }
Show Detail routing
Now, we will implement an API that will be called with the user ID and display the details of the corresponding user.
var express=require('express'); var app=express(); var fs=require("fs"); app.get('/:id', function (req, res) { //First read existing users. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { var users=JSON.parse( data ); var user=users["user" + req.params.id] console.log( user ); res.end( JSON.stringify(user)); }); }) var server=app.listen(8081, function () { var host=server.address().address var port=server.address().port console.log("Example app listening at http://%s:%s", host, port) })
Now try using any REST client to use the URL on the local machine: http://127.0.0.1:8081/2 And HTTP methods: GET accesses the defined API. This should produce the following output-
{"name":"suresh","password":"password2","profession":"librarian","id":2}
Delete User route
This API is very similar to addUser API. In addUser API, we use req The body receives the input data and then deletes the user from the database according to the user ID. To simplify the process, we assume that we want to delete the user with ID 2.
var express=require('express'); var app=express(); var fs=require("fs"); var id=2; app.delete('/deleteUser', function (req, res) { //First read existing users. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { data=JSON.parse( data ); delete data["user" + 2]; console.log( data ); res.end( JSON.stringify(data)); }); }) var server=app.listen(8081, function () { var host=server.address().address var port=server.address().port console.log("Example app listening at http://%s:%s", host, port) })
Now try using any REST client to use the URL on the local machine: http://127.0.0.1:8081/deleteUser And HTTP methods: DELETE accesses the defined API.
{"user1":{"name":"mahesh","password":"password1","profession":"teacher","id":1},"user3":{"name":"ramesh","password":"password3","profession":"clerk","id":3}}