We've explored GET and POST, and now it's time to deal with another foundational REST verb: DELETE.
As its name implies, its goal is to remove data from a data store. We already have our button to do so, so let's wire it up:
- In our frontend JavaScript, we'll add the following:
const deleteData = () => {
fetch('/', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: 'message' })
})
}
document.querySelector('#delete').addEventListener('click', () => {
deleteData()
window.location = "/"
})
- Now, in the routes, add this route:
/* DELETE from json and return to home page */
router.delete('/', function(req, res) {
store.del(req.body.id);
res.sendStatus(200);
});
And that should be all we need. Refresh your index page and play around with the add and delete buttons. Pretty easy, right? In Chapter 18, Node.js and...