Node.JS RESTful API

REST stands for REpresentational State Transfer. REST is a standard web base architecture that relies on HTTP protocol. Simply put, REST treats every component of the web application as a resource which can be accessed via common HTTP interface. REST architecture is based upon server and client. A client requests for a resource to REST server via HTTP, REST server provides client, access to that resource. Each rest resource is identified by a global ID or a URL. A REST resource can be presented in the form of JSON, XML or simple text. However, JSON is the preferred representation. In this article we shall study how to implement node.js RESTful API architecture.

Node.js RESTful API Methods

The RESTful API methods are HTTP methods commonly used in RESTful APIs. There are four major HTTP methods. 1) Get: It provides read only access to HTTP resource. 2) PUT: It creates a new resource. 3) Delete: This method is used to delete an existing resource.

RESTful Web Services

Web services provide communication facilities between applications written in different languages and deployed on different platforms. Web services make use of some open web standards used for communication between different devices. RESTful web services are web services that implement node.js RESTful API architecture.

Creating a RESTful API

Enough of theory, let’s jump into some coding now. Suppose we have a JSON database of student records which looks like this.

{
   "Student1" : {
      "name" : "John",
      "Age" : 10,
      "Gender" : "Male",
      "id":1
   },
  "Student2" : {
      "name" : "Susan",
      "Age" : 9,
      "Gender" : "Female",
      "id":2
   },
  "Student3" : {
      "name" : "Josh",
      "Age" : 11,
      "Gender" : "Male",
      "id":3
   },
  "Student4" : {
      "name" : "Sofia",
      "Age" : 8,
      "Gender" : "Female",
      "id":4
   }
}

Download the file

Now we have a resource named students.json. We shall now provide HTTP methods so that clients can access this resource in different ways. We will provide four HTTP methods for this resource.

  • listStudents: This method will use HTTP get and will display list of all students.
  • addStudent: This method adds a new student via POST method.
  • deleteStudent: This method deletes a student via HTTP delete
  • :Name: Retrieves record of a student.

Implementing listStudents Method

The first method that we shall implement is the listStudent method. But before that, make sure you have a global installation of express module since this application makes use of it. If you do not have express module installed. Type the following command in your node.js command window “npm install express –save”. This will install express module globally. Now, take a look at the following code.

var express = require('express');
var expapp = express();
var fileSystem = require("fs");

expapp.get('/listStudents', function (req, res) {
   fileSystem.readFile( __dirname + "/" + "students.json", 'utf8', function (err, data) {
      console.log( data );
      res.end( data );
   });
})

var rserver = expapp.listen(9006, function () {
   var hostaddr = rserver.address().address
   var portaddr = rserver.address().port

   console.log("Example app listening at http://%s:%s", hostaddr, portaddr)
})

Download the code Try the code

In the above code, we implement the get method of the express module. This method is similar to HTTP get. This method makes server respond with contents of students.json file whenever a request arrive for listStudents. Run this code and then type http://127.0.0.1:9006/listStudents. You shall see all the student records printed in your browser.

In the next tutorial, we shall implement the addStudent, deleteStudent and :Name methods. Keep visiting this site.

<<< Node.JS Express Framework IINode.JS RESTful API II >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .