Currently, I'm trying to use oAuth service so I needed to get the params from website and i need to give this params Info to Backend, So, I tried to pass it by using fetch
from react.js,
However, I get the empty object in backend , i want to know how I can get the params and what make it empty .
Thank you in advance
Here is my code :
client:
useEffect(() => {
fetch("http://localhost:5000/create", {
method: "POST",
body: JSON.stringify(code),
})
.then(response => response.json())
.then(json => {
console.log("parsed json", json);
})
.catch(ex => {});
});
server:
app.post("/create", function (req, res) { console.log(req.body); });
CodePudding user response:
GET /something?color1=red&color2=blue
app.get('/something', (req, res) => {
req.query.color1 === 'red' // true
req.query.color2 === 'blue' // true
})
// GET /search?q=tobi ferret
console.dir(req.query.q)
// => 'tobi ferret'
// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
console.dir(req.query.order)
// => 'desc'
console.dir(req.query.shoe.color)
// => 'blue'
console.dir(req.query.shoe.type)
// => 'converse'
// GET /shoes?color[]=blue&color[]=black&color[]=red
console.dir(req.query.color)
// => ['blue', 'black', 'red']
I hope this may help you.**strong text**
CodePudding user response:
You have to use express body-parser middleware.
app.use(express.json());