I have a simple blog site using EJS, Express, and Axios. No frontend framework - plain JS and EJS. I'm still relatively new to Express and Axios.
Trying to do a simple POST
request with Axios to the router file router.post("/admin/posts/create-post", adminCreatePost)
and get a POST http://localhost:3000/admin/posts/create-post 404 (Not Found)
error in the browser console.
The adminCreatePost
function is from a separate controller file. I commented out all code and put a simple console.log("hello from the controller")
in the controller and it does log the message, but still get the 404
error in the browser console. So the adminCreatePost
required in doesn't seem to be the issue.
I've triple checked the url
I pass in to Axios POST
call. I tried a simple GET
request with Axios to make sure Axios itself wasn't the problem - the GET
request worked.
I'm sure it's probably something simple/silly I'm not thinking about or overlooking. Any thoughts?
If you need more info, happy to answer.
savePost.js on the frontend
document.getElementById('save-post-button').addEventListener('click', (event) => {
event.preventDefault();
let postContent = [];
let textAreas = document.getElementsByTagName('textarea');
const arrayOfTextAreas = Array.prototype.slice.call(textAreas);
arrayOfTextAreas.forEach(element => {
const block = new Object();
block.type = element.dataset.blockType;
block.contents = element.value;
console.log(block);
postContent.push(block);
})
let newPost = {
postReadyToView: Boolean(document.getElementById('new--ready-for-viewers').value),
postPath: document.getElementById('new--post-path').value,
postTitle: document.getElementById('new--post-title').value,
postDate: document.getElementById('new--email-subscribers-date').value,
postUpdateDate: null,
postContent: postContent
};
axios.post("/admin/posts/create-post", newPost)
.then(res => console.log(res))
.catch(error => console.log(error))
})
router.js on backend
const {postREQ_adminCreatePost } = require("./controllers/controllerAdmin");
router.post("/admin/posts/create-post", postREQ_adminCreatePost)
module.exports = router;
controllerAdmin.js on backend
exports.postREQ_adminCreatePost = (req, res, next) => {
console.log('hello from controller');
next();
}