RESTful API’s in node.js

Asirinaidu P
Jul 24, 2017 · 3 min read

Representational State Transfer is a web standard it uses HTTP protocol as interface to communicate with web resources. REST server provides access to resources and REST client get access and modification of resources using HTTP. REST uses various types to represent its resources they are like JSON, text or XML.

HTTP Methods:

REST server ready to accept different types of API’s calls from REST client, each call is of different request method category. They are

GET: This method is for accessing or read only access to resource

PUT: For creating new resource put will help

DELETE: Delete method will delete a resource

POST: This method can be used in two different contexts they are like updating existing resource or creating new resource

RESTful Webservice:

Webservice is a collection of protocols and standards used to exchange data between computer applications or systems. Webservices which are based on REST Architecture are called as RESTful webservices. RESTful webservice provides a URI which will give access to resources.

Example RESTful Service:

Now consider an example that we have users.json data in the form of JSON

{

“user1” : {

“name” : “abc”,

“password” : “password1”,

“profession” : “Engineer”,

“id”: 1

},

“user2” : {

“name” : “xyz”,

“password” : “password2”,

“profession” : “Doctor”,

“id”: 2

}

}

Let’s implement our first RESTful API to show list of users using our list_users.js script below

var express = require(‘express’);

var app = express();

var fs = require(“fs”);

app.get(‘/listUsers’, function (req, res) {

fs.readFile(“./users.json”, ‘utf8’, function (err, data) {

console.log( data );

res.end( data );

});

})

var server = app.listen(8080, function () {

var host = server.address().address

var port = server.address().port

console.log(“My first application showing at http://%s:%s", host, port)

})

Accessing API:

Consider any of the REST client and access the url http://localhot:8080/listUsers you will get the below response.

{

“user1” : { “name” : “abc”, “password” : “password1”, “profession” : “Engineer”, “id”: 1 },

“user2” : { “name” : “xyz”, “password” : “password2”, “profession” : “Doctor”, “id”: 2 }

}

Adding new user:

Write an api for adding new user, here it is mandated to add an URI.

var express = require(‘express’);

var app = express();

var fs = require(“fs”);

var user = {

“user3” : {

“name” : “pqr”,

“password” : “pass”,

“profession” : “lawyer”,

“id”: 3

}

}

app.post(‘/addUser’, function (req, res) {

fs.readFile(“./users.json”, ‘utf8’, function (err, data) {

data = JSON.parse( data );

data[“user3”] = user[“user3”];

console.log( data );

res.end( JSON.stringify(data));

});

})

var server = app.listen(8080, function () {

var host = server.address().address

var port = server.address().port

console.log(“ My first application showing at http://%s:%s", host, port)

})

After successful operation of adding a record to data base user to able to retrieve all the available records.

Consider any of the REST client and access the url http://localhot:8080/listUsers you will get the below response.

{

“user1” : { “name” : “abc”, “password” : “password1”, “profession” : “Engineer”, “id”: 1 },

“user2” : { “name” : “xyz”, “password” : “password2”, “profession” : “Doctor”, “id”: 2 },

“user3” : { “name” : “pqr”, “password” : “pass”, “profession” : “lawyer”, “id”: 3 }

}

Same as like adding and accessing we do have other calls update and delete, these two API can be constructed to delete and update a resource.

Do let me know your thoughts writing an email to me at Asirinaidu.Paidi@suneratech.com or use the comments section below.

For more details on software applications development and support, visit our website — http://www.suneratech.com/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade