I'm creating my own API (with express) and react.js application. However, when I send the data from the react app, it is not retrieved by the server. Here's my code:
POST Request Client Side:
async function sendRequest(text) {
const url = "http://localhost:51515/initialize"
const settings = {
method: "POST",
body: {
text: text
}
}
try {
const response = await fetch(url, settings)
return await response.json()
} catch(err) {
console.log(err)
}
}
Server Side POST Request Retrieval:
const express = require("express");
const app = express();
const PORT = 51515
app.use(express.json())
app.listen(
PORT,
() => console.log(`API initialized on http://localhost:${PORT}`)
)
app.post('/initialize', (req,res) => {
console.log(req.body) // The log output is {}
})
Do you know how I can fix this?