Fetch request through an HTML page. I want to post formData object to the server which is hosted locally.
let formData={
name:document.getElementById('name').value,
question:document.getElementById('question').value
}
let response=await fetch('http://localhost:5000/',{
method:'POST',
mode:'no-cors',
headers:{'Content-Type':'application/json'},
body:JSON.stringify(formData)
}).then((res)=>console.log(res)).catch((err)=>console.log(err))
})
The req.body gives an empty object, I also tried using body-parser but it didn't work either.
//--------app.js-------------
const express = require('express');
const app=express();
const port=process.env.PORT || 5000;
app.use(express.json());
app.get('/',(req,res)=>{
res.send('im alive')
})
app.post('/',(req,res)=>{
console.log(req.body);
res.json({status:"okay"});
})
app.listen(5000,()=> console.log('Listening at '+port));