Node.JS RESTful API II

In the previous article, we started discussing node.js RESTful API. We saw what RESTful API is and how to implement HTTP methods to access RESTful API resources. We created a sample student database in JSON in the form of students.json file. We then used express module’s get function to create RESTful API’s listStudents method which retrieved list of all the students. However, we did not implement the addStudent, deleteStudent and :name methods. In this article we shall implement these methods.

AddStudent Method

It is very straight forward to add a user to already populated JSON database. Take a look at the following code snippet.

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

var Student = {
    "Student5" : {
      "name" : "Wasim",
      "Age" : 12,
      "Gender" : "Male",
      "id":5
   }
}

expapp.get('/addStudent', function (req, res) {
  
   fileSystem.readFile( __dirname + "/" + "students.json", 'utf8', function (err, data) {
      data = JSON.parse( data );
      data["Student5"] = Student["Student5"];
      console.log( data );
      res.end( JSON.stringify(data));
   });
})
var server = expapp.listen(9006, function () {
   var host = server.address().address
   var port = server.address().port
   console.log("Server running at http://%s:%s", host, port)
})

Download the code Try the code

Take a look at the above code. Here we again use the “get” method of the express class. This method takes the the path of the RESTful method i.e. “/addStudent” as the first parameter. The second parameter is the callback function that executes whenever a user requests for “/addStudent” path. Notice that in the code we have created a new student object i.e. “Student5”. When a user requests for “/addStudent” path. The “Student5” object is added to the student database which is consequently printed on the screen. Run the above code and then type the following URL in your browser “http://127.0.0.1:9006/addStudent”. You shall see five students printed on the screen.

Show Student Method

The code for displaying detailed record of a student based upon the “id” attribute is as follows:

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

expapp.get('/:id', function (req, res) {
 
   fileSystem.readFile( __dirname + "/" + "students.json", 'utf8', function (err, data) {
      data = JSON.parse( data );
      var student = data["Student" + req.params.id] 
      console.log( student );
      res.end( JSON.stringify(student));
   });
})

var server = expapp.listen(9006, function () {
   var hostaddr = server.address().address
   var portaddr = server.address().port
   console.log("Example app listening at http://%s:%s", hostaddr, portaddr);
})

Download the code Try the code

Take a look at the above code. Here we again use the “get” method of the express class. This method takes the the id of the student i.e. “/:id” as the first parameter. The second parameter is the callback function that executes whenever a user requests for “/id”. Inside the function, the details of JSON’s Student object whose value for id attribute matches the id passed in the path, shall be displayed. For instance run the above code and then type the following URL in the browser “http://127.0.0.1:9006/3”. This will display the record of student with id 3.

Delete Student

The following code deletes student with id three from JSON student data. Take a look at it.

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

var id = 3;

expapp.get('/deleteStudent', function (req, res) {
   fileSystem.readFile( __dirname + "/" + "students.json", 'utf8', function (err, data) {
      data = JSON.parse( data );
      delete data["Student" + id];
       
      console.log( data );
      res.end( JSON.stringify(data));
   });
})

var rserver = expapp.listen(9006, function () {
   var hostaddr = rserver.address().address
   var portaddr = rserver.address().port
   console.log("Server running at http://%s:%s", hostaddr, portaddr)
})

Download the code Try the code

Run the above code and then type this url in the browser: “http://127.0.0.1:9006/deleteStudent”. You will not see student with id 3, among the students record.

<<< Node.JS RESTful APINode.JS OS Module >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .