1.

Explain the term statelessness with respect to RESTful web Services. Write its advantages.

Answer»

Statelessness is basically a condition or restriction where RESTful web services are not allowed to keep a client state on the SERVER as per the REST architecture. Clients are responsible to pass their context to the server. To process the client’s request, the server then further stores this context.  

ADVANTAGES 

  • No need to maintain previous interactions with clients.
  • Independent treatment of each method request.
  • Less complexity and simplified application design.

Example: Simple GET Request using NodeJS

We have the following sample data in users.json file.
Filename: users.json

{ "user1" : { "name" : "gourav", "password" : "password1", "PROFESSION" : "officer", "id": 1 }, "user2" : { "name" : "nikhil", "password" : "password2", "profession" : "teacher", "id": 2 }}

Implement our RESTful API listUsers using the following code in a server.js file.
Filename: server.js

// Requiring modulevar express = require('express');var app = express();var fs = require("fs");// Sample GET APIapp.get('/listUsers', function (req, res) { fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { console.log( data ); res.end( data ); });})// Server setupvar 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 open a browser and go to http://127.0.0.1:8081/listUsers,  we will get the following response:

{ "user1" : { "name" : "gourav", "password" : "password1", "profession" : "officer", "id": 1 }, "user2" : { "name" : "nikhil", "password" : "password2", "profession" : "teacher", "id": 2 }}


Discussion

No Comment Found