I am trying to sent my form data to node js and retrieve at there.But I am failed to do it with form data object.
handleSubmit(data) {
console.log(data);
const forms=new FormData();
forms.append('fname','pranab');
let promise=fetch('http://localhost:8080/reactTest', {
method:'POST',
mode:'CORS',
body:JSON.stringify(forms),
headers:{
'Content-Type':'application/json',
'Accept':'application/json'
}
}).then(res =>res.json()).then(result => console.log(result))
}
in the above code I am sending form data object to the serve side.At the server side I am trying to retrieve it using below code.
app.post('/reactTest',function(req,res) {
var contents=req.body;
console.log(contents);
return res.send({status:'working'});
})
but it displays nothing in the console.What the issue here is?
output is
titech@titech-System-Product-Name:/var/www/html/nodejs$ node index.js
port listening on 8080
{}
when i trying to console it with 'req.body.fname' it gives 'undefined' as output in place of '{}'.