Today for my quick morning post on express.js I wanted to start taking a look at some of the other http request methods other than get, and post. So for today I put together a quick demo that makes use of the app.delete method. This is a express app object method that can be used to define what to do for an http delete request in express.
This is a post on the app.delete method of the app object in express.js. This is a method that is used to define logic that is used to handle http 1.1 delete requests. This is not a getting started post on express.js, or any additional skills that are required before hand to get something of value from this. If you are new to express you might want to check out my main post on express.
The setup process was not all that different from many of my other examples on express. I just created a new folder, made it the current working folder. I also made a public folder to house a simple static client with express.static. Also I set up a routes folder as a way to keep things a little more organized compared to having everything in the main app.js file.
This file includes methods for making a get request for the file, making a post request to create or overwrite the file, and of course a method to make the delete request.
In this example I have made a file for each type of request that is used including GET, POST, and yes of course DELETE.
This file will create a /post path that will respond to post requests that will be made by the client system. It will just check for a body, and message, and if all goes well will write a given message to a file called file.txt in the routes folder.
The file.txt file will of course be the file in question that will be served up via GET requests, and deleted when a delete request is received.
This is a file that will handle GET requests made to a /file path. If the file is there it will respond with it’s contents. If he file is not there it will respond with a message that indicates that.
This of course is the file that will respond to DELETE requests from the client. For this simple example I am not doing anything fancy, there is only one file to delete, and it will just be deleted. In an actual production app this might be handled differently, but the basic idea is there.
Because I have placed the bulk of the examples logic off into the additional files in the route folder, the size of my main app.js file is very small. Here I am just using app.use to use each of the route files to handle GET requests for file.txt, post requests to create or overwrite file.txt, and delete requests to delete file.txt.
When I start the app from the command line with node like usual
The express app starts listening on port 8080, so I can navigate to it in the browser by going to localhost:8080.
When doing so I can write something in the textarea element, click post, and the text will be saved as the contents of file.txt. If it is not there it will be created, if it is there it will be overwritten. In any case a get request is then made to the /file path each time the browser reloads, or when a POST or DELETE request is made to confirm that this works.
Of course clicking the delete button will make a delete request, and file.txt will then be deleted.
Thats if for now, I just wanted to make a quick app.delete example for today, as I continue to expand my content on express. If this post sparks some interest, maybe I will revise it to get deeper into the DELETE http method, and why it may be a better choice compared to using POST.