RESTful API’s in node.js

RESTful API’s in node.js

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 https://%s:%s", host, port)

})

Accessing API:

Consider any of the REST client and access the url https://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 https://%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 https://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 [email protected] or use the comments section below.

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



要查看或添加评论,请登录

AsiriNaidu Paidi的更多文章

  • AUTOMATING BUSINESS WORKFLOW WITH CLOUDTESTR(White Paper)

    AUTOMATING BUSINESS WORKFLOW WITH CLOUDTESTR(White Paper)

    Overview Cloudtestr is an automation framework, facilitating users to test their application in the cloud platform, so…

  • Opportunities of Big Data that are big in demand!

    Opportunities of Big Data that are big in demand!

    The term BigData describes a huge amount of data that comes in multiple forms like structured, un-structured and semi…

  • IMPORTANCE OF DATA SCIENCE AND ITS BENEFITS

    IMPORTANCE OF DATA SCIENCE AND ITS BENEFITS

    Data science, a combination of multiple studies like technology, algorithm development and data inference to solve…

  • WHAT IS AGILE? HOW TO GET MORE VALUE OUT OF IT?

    WHAT IS AGILE? HOW TO GET MORE VALUE OUT OF IT?

    Agile is a software development model, it refers to a group of software methodologies based on iterative development…

  • 5 Steps Towards Digital Transformation

    5 Steps Towards Digital Transformation

    Digital Transformation is the change associated with businesses and organizational processes, and opportunities towards…

  • RabbitMQ, A Message Broker for Python

    RabbitMQ, A Message Broker for Python

    RabbitMQ is one of the widely used message brokers. It accepts messages, stores and sends messages to destination.

    2 条评论
  • Quick View Of Django Rest Framework

    Quick View Of Django Rest Framework

    Django REST Framework or formally called as DRF is a powerful and flexible package for building Web APIs. There are…

    1 条评论
  • Quick Look At Pentesting

    Quick Look At Pentesting

    Are you often listening the term Security Breach in the news or social channels? Been seeing a lot of cases where…

  • MVC Patterns in AngularJS

    MVC Patterns in AngularJS

    AngularJS is an Opensource and Powerful JavaScript Library. It is widely using in Single Page Applications.

  • File System Operation in node.js

    File System Operation in node.js

    Node.js is opensource, completely free and powerful JavaScript library.

社区洞察

其他会员也浏览了