send from post man
that will place into my json
then it will submit ournew json
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.put("/persond",(req,res,next) =>
{
let _body=req.body;
let person_list=[
{name:"huderee",addr:"vamsi1",id:1},
{name:"hudere",addr:"vamsi2",id:2},
{name:"hudere",addr:"vamsi3",id:3}
];
let index=person_list.findIndex(x=>x.id==_body.id);
if(index !=-1)
{
person_list[index]=_body;
}
res.send(person_list);
})
total code
const express= require('express');
const app = express();
const PORT=3000;
app.get("/person",(req,res,next) =>
{
let person={name:"hudere",addr:"vamsi",id:1}
res.send(person);
}
);
app.get("/persons/:id",(req,res,next) =>
{
console.log(req);
let idd=req.params.id;
let person=[
{name:"hudere",addr:"vamsi1",id:1},
{name:"hudere",addr:"vamsi2",id:2},
{name:"hudere",addr:"vamsi3",id:3}
];
person=person.filter(x=>x.id==idd);
res.send(person);
}
)
app.get("/person_with_query",(req,res,next) =>
{
let query=req.query;
let person=[
{name:"huderee",addr:"vamsi1",id:1},
{name:"hudere",addr:"vamsi2",id:2},
{name:"hudere",addr:"vamsi3",id:3},
{name:"hudere",addr:"vamsi3",id:1}
];
person=person.filter(x=>x.id==query.id & x.name ==query.name);
res.send(person);
})
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.put("/persond",(req,res,next) =>
{
let _body=req.body;
let person_list=[
{name:"huderee",addr:"vamsi1",id:1},
{name:"hudere",addr:"vamsi2",id:2},
{name:"hudere",addr:"vamsi3",id:3}
];
let index=person_list.findIndex(x=>x.id==_body.id);
if(index !=-1)
{
person_list[index]=_body;
}
res.send(person_list);
})
app.listen(PORT,() =>
{
console.log("server on::"+PORT)
}
)
in put method
we will send some data to server in request body
whatever we recieved we wil take and update in our json
we write in raw as json inputs
method is put
and if u look at code
take the requested data into body variabvle
we already have json wite into person_list
from requested body find that id in json
if matches
update at that index
send to post man back
if u forget json parser it will show error requested body not defined
No comments:
Post a Comment