Im very new to servers and node js and js but I am trying to construct a dynamic web page. On this webpage I have a comment form where people can leave a comment. My problem is that on the server-side this comment form works fine but I am trying to implement some client-side js so that when they submit a form if an error occurred (e.g. not all fields were filled) or if no error occurred an alert appears on the webpage saying an appropriate message for either case. I also want the form to clear after the submit button has been pressed and the details have been saved to the server if there was no error. Currently the form resets before the details save to the server so in the server all the fields saved are blank and I get this error:
GET http://127.0.0.1:3000/comment 404 (Not Found)
handleFormSubmit @ main.js:43
this is my code : Index.html
<html>
<head>
</head>
<body>
<h1>Comment!</h1>
<form action='http://localhost:3000/comment' method='POST' id='CommentSection'>
<div class='form-group'>
<label for='username'>username</label>
<input class='form-control' name='username'>
</div>
<div class='form-group'>
<label for='Title'>Info</label>
<input class='form-control' name='title' >
</div>
<div class='form-group'>
<label for='Comment'>Comment</label>
<input class='form-control' name='comment' >
</div>
<button type='submit' class='btn btn-primary'>Submit</button>
</form>
<script src='client.js'></script>
</body>
</html>
server.js
let AllComments=[];
app.post('/comment', (req, res) => {
const com = req.body;
console.log(com);
AllComments.push(com);
res.status(201);
});
client.js
async function commentSubmit(event) {
fetch('http://127.0.0.1:8090/comment');
alert('success');
CommentSection.reset();
}
const CommentSection = document.getElementById("CommentSection");
commentForm.addEventListener("submit", commentSubmit);