I am able to retrieve data from express but I am facing issues when trying to post data to express...
client:
<html>
<button onclick="myFunction()">send</button>
<script>
const data = {"experience" : 0};
function myFunction(){
fetch("/post", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
}
</script>
</html>
express:
Initially, I was getting an undefined response, but after adding express.json(), the response changed to "{}". Both client and server connections seem fine, however, there doesn't appear to be a body where the data is stored. I tested my client code by posting data to webhooks.site, and it worked perfectly. I believe there might be a simple mistake somewhere... By the way, I am using react and express, although I have simplified my code here. Any suggestions would be highly appreciated.
const express = require("express");
const app = express();
app.post("/post", express.json() ,function (req,res){
console.log(req.body)
})
const port = 5000;
app.listen(port, () => `Server running on port ${port}`);