I am using React and Express to try and post an Article to MongoDB after clicking a button.
**server.js**
app.use(express.static("public"));
app.post("/articles/:id", function(request, response){
console.log(request.body);
});
and
**home.jsx**
addToFavorites = article => {
console.log(article);
this.state.savedArticles.push(article);
this.setState(this.state.savedArticles);
axios.post("/articles/" + article.id, {
title: article.title,
summary: article.summary,
writer: article.writer,
date: article.pub_date,
url: article.link
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
The console prints out the article so it is not undefined and the call catches the following error :
Error: Request failed with status code 404
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:78)
Reading other posts, people have mentioned that the path does not exist, but I'm not sure what that means.
Any help would be greatly appreciated :)
UPDATE :
My main issue was just that I did not run node server.js before yarn start. I am new to React so I did not know that this was important.
Including the proxy in package.json was also important.