I’m making a CRUD application and I’m making an update comment function. Normally, there is a patch request that is used for updating or modifying objects. However, while working on forms, I saw that the html forms only respond to GET and POST requests. This got me thinking for the UPDATE application of the CRUD, and I thought I could simply use a POST request to create the application.
html:
<form action="http://localhost:3000/update" id='POST' method="post">
<label for="username">Username: </label>`
<input type="text" name='username' id='username'>
<label for="comment">Comment:</label>
<input type="text" name="comment" id="comment">
<button>UPDATE</button>
</form>
Javascript:
app.post('/update', (req, res) => {
console.log(req.body);
const { username, comment } = req.body;
const ind = comments.findIndex((element) => element.username === username)
comments[ind].comment = comment;
console.log('Comment updated!');
res.redirect('/comments');
})
The code works as expected on my end, it updates the person’s comment and then redirects to /comments, which is a page that displays all comments, but I’m not sure if it’s good practice to do it like this.