Demonstration of PUT method using NodeJS and ExpressJS

Rajnilari2015
Posted by Rajnilari2015 under Node.js category on | Points: 40 | Views : 408
In the below code, we will find an example that demonstrates the use of PUT method using NodeJS and ExpressJS

//add the express module
var express = require('express');

//create an instance of express module
var app = express();

//prepare the Employee data source/model
var employees =
[
{
"EmployeeID" :1 ,
"EmployeeName" : "RNA Team",
"Salary" : "200000",
"Address" : "Bangalore"
},
{
"EmployeeID" :2 ,
"EmployeeName" : "Mahesh Samabesh",
"Salary" : "100000",
"Address" : "Hydrabad"
},
{
"EmployeeID" :3 ,
"EmployeeName" : "Rui Figo",
"Salary" : "50000",
"Address" : "Dallas"
},
{
"EmployeeID" :4 ,
"EmployeeName" : "Indradev Jana",
"Salary" : "456789",
"Address" : "Los Angles"
},
{
"EmployeeID" :5 ,
"EmployeeName" : "Suresh Shailesh",
"Salary" : "1234567",
"Address" : "Patna"
}
];


//update Employee Record
app.get('/:EmployeeID/:Salary/:Address', function (req, res) {

var employeeID = req.params.EmployeeID;
var employeeSalary = req.params.Salary;
var employeeAddress = req.params.Address;
console.log(employeeID + "-------" + employeeSalary +"--------" + employeeAddress);

//Get Employee Records whose EmployeeID = get the EmployeeID at runtime

for(var i= 0; i< employees.length; i++){

if(employees[i].EmployeeID == employeeID){

employees[i].Salary = employeeSalary;
employees[i].Address = employeeAddress;
}
} //end Loop
res.send(employees);
});

//run the server
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;

console.log('Server started and is listening at :> http://%s:%s', host, port);
});

Comments or Responses

Login to post response