using splice method
remove an element from array by index
app.delete("/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.splice(index,1);
}
res.send(person_list);
})
send the data to nodejs applicatiaon via rest api
methiod is delete
how to remove key from json
person_list.splice(index,1);
what id will send , taht will removed from json and send to client
post method in json
app.post("/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);
let response;
if(index !=-1)
{
response='id is already there'
} else
{
person_list.push(_body);
response=person_list;
}
res.send(response);
})
post medhod
postt the data it wil psush to array
app.post("/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);
let response;
if(index !=-1)
{
response='id is already there'
} else
{
person_list.push(_body);
response=person_list;
}
res.send(response);
})
total code of create get update delete
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');
let multer = require('multer');
let upload = multer();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/sayHello', upload.array(), (request, response) => {
let a = request.body.a;
let b = request.body.b;
let c = parseInt(a) + parseInt(b);
response.send('Result : '+c);
console.log('Result : '+c);
});
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.delete("/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.splice(index,1);
}
res.send(person_list);
})
app.post("/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);
let response;
if(index !=-1)
{
response='id is already there'
} else
{
person_list.push(_body);
response=person_list;
}
res.send(response);
})
app.listen(PORT,() =>
{
console.log("server on::"+PORT)
}
)
No comments:
Post a Comment