Reputation: 53
React js:
export const insertUser=( name, facebookId )=> {
return (dispatch) => {
fetch("http://localhost:3000/insertUser", {
method: "post",
credentials: 'same-origin',
mode: 'no-cors',
redirect: 'follow',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Origin': '',
},
body: JSON.stringify({
facebookId: facebookId,
name: name
}),
}).then((response) => {
console.log(response)
});
}
}
Node js express:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/insertUser, user.createUser);
The react js code sends empty body to the backend. If I make the mode: cors, I get a 404.
Upvotes: 1
Views: 129
Reputation: 81
You are passing data as string and you have configured bodyparser using json.
When you are passing data as string then you should use as text like this :
app.use(bodyParser.text());
Upvotes: 1