I am sending json data from react application using fetch API but in server I'm getting req.body as empty.What's the problem I don't know
Here is my codeReact code
import './App.css';
function App() {
function handleClick() {
// Send data to the backend via POST
fetch('http://localhost:8000/message', { // Enter your IP address here
method: 'POST',
header:{'Content-Type':'application/json'},
body: JSON.stringify({name:"ROHIT"}) // body data type must match "Content-Type" header
})
}
return (
<div className="App">
TO DO App by Rohit Satpute
<button onClick={handleClick} style={{
}}>
Send data to backend
</button>
</div>
);
}
export default App;
const express=require('express');
const cors=require('cors');
const mongoose=require('mongoose');
const MySchema=require('./myschema.js');
const app=express();
app.use(cors());
app.use(express.json());
app.post('/message',(req,res)=>{
console.log(req.body);
res.json("rohit");
})
app.listen(8000,()=>{
console.log("server is running on port 8000");
})
I tried to find in what format does the fetch API is sending data to server.But in req of server it is nowhere.