React code - FileUploader.js
// React - FileUplader.js
const handleSubmission = (e) => {
e.preventDefault();
if(isSelected === false){
alert("load the file");
}
else{
const formData = new FormData();
formData.append("certificate",selectFile);
// API CALL
fetch("http://localhost:8080/upload", {
method: "POST",
body: formData,
headers : {
"Content-Type" : "multipart/form-data"
}
}).then((response) =>response.json())
.then((result)=>{
console.log("Success : ", result);
})
.catch((error)=>{
console.error("Error : ",error);
});
}
};
Nodejs code - Server.js
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.post('/upload', async function(req ,res){
try {
const file = req.files; // undeifined
const bodyData = req.body; // {}
console.log("file : ",file);
console.log("bodyData : ",bodyData);
res.status(200).send({
message: "FILE RECEIVED!"
});
} catch(error){
res.send("ERROR")};
});
Problem
Why req.body is {} in node js
I tried using MULTER but got the same result
MDN says that FormData object is not a simple object, but a special object created for XMLHttpRequest transmission and cannot be recorded with the console.