Adding Data

Following API will show you how to add new user in the list. Following is the detail of the new user

user = {

“user4” : {

“name” : “mohit”,

“password” : “password4”,

“profession” : “teacher”,

“id”: 4

}

}

You can accept the same input in the form of JSON using Ajax call but for teaching point of view, we are making it hard coded here. Following is the addUser API to a new user in the database −

server.js

var express = require(‘express’);

var app = express();

var fs = require(“fs”);

var user = {

“user4” : {

“name” : “mohit”,

“password” : “password4”,

“profession” : “teacher”,

“id”: 4

}

}

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

// First read existing users.

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

data = JSON.parse( data );

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

console.log( data );

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

});

})

var 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 try to access defined API using URL: http://127.0.0.1:8081/addUser and HTTP Method : POST on local machine using any REST client. This should produce following result −

{

“user1”:{“name”:”mahesh”,”password”:”password1″,”profession”:”teacher”,”id”:1},

“user2”:{“name”:”suresh”,”password”:”password2″,”profession”:”librarian”,”id”:2},

“user3”:{“name”:”ramesh”,”password”:”password3″,”profession”:”clerk”,”id”:3},

“user4”:{“name”:”mohit”,”password”:”password4″,”profession”:”teacher”,”id”:4}

}

Share this post
[social_warfare]
Listing Data
Showing Detail

Get industry recognized certification – Contact us

keyboard_arrow_up