Backend newbie here! I am trying to post from my frontEnd js to Node, but the body of the request always come in empty. I have read tons of answers here, but none helped me. this is my js trying to post:
const sendTest = () => {
const data = createObject();
if (data == false) {
alert("fill everything!");
} else {
console.log(JSON.stringify(data));
const parameters = {
method: "POST",
body: JSON.stringify(data),
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
};
fetch("http://127.0.0.1:3035/api/sendSoftSkills", parameters);
}
};
JSON.stringfy(data):
{"ss1":"1","ss2":"1","ss3":"1","ss4":"2","ss5":"2","ss6":"2","ss7":"2","ss8":"2","ss9":"2","ss10":"2","ss11":"2","ss12":"1","ss13":"2","ss14":"1","ss15":"2","ss16":"1","ss17":"2","ss18":"1","ss19":"1","ss20":"1"}
on my node.js with express:
app.post("/api/sendSoftSkills", (req, res) => {
console.log("POSTED!");
console.log(req.body);
});
my terminal:
POSTED!
{}
I am using body-parser middleware. Am I missing something obvious? Any help appreciated, thank!
You need to parse the body:
app.use(express.json())
app.post("/api/sendSoftSkills", (req, res) => {
console.log("POSTED!");
console.log(req.body);
});