in requesting url if we have query params
how to deal with that
query takes req,query
if we have matched that with two ids, it wil print two id jsons
app.get("/person_with_query",(req,res,next) =>
{
let idd=req.query.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);
})
whole code
const express= require('express');
const app = express();
const PORT=3000;
app.get("/person",(req,res,next) =>
{
let person={name:"hudere",addr:"vamsi"}
res.send(person);
}
);
app.get("/persons/:id",(req,res,next) =>
{
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 idd=req.query.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.listen(PORT,() =>
{
console.log("server on::"+PORT)
}
)
what it will print
how to write two conditions based on query
we wil take query params as query and
in filter write with two conditiions
http://localhost:3000/person_with_query?id=1&name=hudere
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);
})
req.query= from request it taks all the query params
then filter based on thta
No comments:
Post a Comment