So, we're almost done.
Let's now make sure we can delete posts.
And that again starts in the post item EJS file,
where I have this button.
Now, there are multiple ways of deleting the post.
Instead of a button, we could use a link,
and then simply add a route for a get request
to the specified path we added to the link,
and then perform our deletion logic.
Alternatively, you could argue that it technically
makes more sense to send a post request to the server
when you want to delete something,
and you can't do this with a link.
That's why I added a button for this.
And then, you can wrap this button into a form, like that,
to make sure that a request is sent
when the button is clicked.
And add an action with the desired path,
and the method of post to this form.
And the desired path could now
be /posts, slash the ID of the post,
which we can inject with help of EJS here, post.id.
As we also did it down there, for example.
/delete.
That could be our path for deleting a post.
And that's one way of ensuring
that a post request is sent to this path
when the delete post button is clicked.
Since this button does not have a type,
it will automatically submit that form,
as you learned before in the course,
and therefore this post request will be sent to this path.
And now in block JS,
we just also have to add a route that handles requests
to this path, post requests to this path.
So here, we can then actually add router.post
and handle the requests to /posts/:id/delete.
And then execute our standard function here.
And in there, in this function,
we now want to delete a post from the database.
We can already convert it into an async function
since we'll again, use async await, because we'll again
send a query to the database.
It will be a pretty short query.
So I'll just write it in line here,
not stored in an extra constant,
which you of course could also do.
And the query we want to send is of course, a query,
a command, we also saw in the last course section already.
We can delete items by using DELETE FROM.
Then the table from where we want to delete something.
And then WHERE, and then id = ?
This is how we can delete all the posts
from the posts table
that match this condition.
And of course,
since we used the ID, and we'll just provide one ID here,
we'll just delete one single post.
So that's then this second parameter value,
which we pass to the query method.
It's this array with all the values that will be used
as replacements for those question marks,
and here that's req.params.id.
That will then delete this post.
Now we cannot wait this operation,
because of course it's an asynchronous one.
And then once this is done, we can, again, redirect,
maybe back to the all posts page like this.
And that is all we have to do.
That's all we have to do in order to delete a post.
Now again, if we save all the files
and I reload this page here, I want to delete a post.
And for this, I'll actually create a new post first.
Test for deletion.
Delete this! Please.
Add to this post.
And now if I click delete post here,
you see, this page is reloaded and the post is gone.
And if I reload it again, it's still gone.
And if I go back, it's still gone.
It's not there.
So deleting, all of it works without errors,
and that's all we have to do here.