I am really confused. I have been doing this for days and can't seem to find what is going on. I am using FormData to upload data to the database and it is always returning empty array when I console.log(req.body); Here is the code
This is the frontend (React app)
import React from 'react';
import logo from './logo.svg';
import './App.css';
const App = () => {
const submit = () => {
const data = new FormData()
data.append("theme", "This is the theme")
fetch("/", {
method: "post",
headers: {
"Content-Type": "multipart/form-data"
},
body: data
}).then(res => res.json())
.then(result => {
console.log(result)
})
}
return (
<div>
<button onClick={ () => submit() }>submit</button>
</div>
)
}
export default App;
And here is the backend
const express = require("express")
const bodyParser = require("body-parser")
const path = require('path')
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(express.static('./public'));
app.post("/", (req, res) => {
console.log(req.body)
})
app.listen(5000, () => {
console.log("Port started on 5000")
})