Express app.delete, to delete a server side file via the http delete method.
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.
1 - Express app.delete and what to know before hand
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.
2 - Setup
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.
|
|
3 - The public folder
The public folder will hold the crude yet effective client system for this example. It consists of just an index.html file, and a single client.js file that will house my front end javaScript code.
3.1 - The /public/index.html
Just a simple html file that will have a textarea message that can be used to define some text that will then be posed to the back end when clicking a post button element. It will also have another button that will make a delete request as well. The javaScript that will power all of this on the front end will be in a client.js file that will be used in the index via a script tag.
|
|
3.2 - The /public/client.js
This file will house the front end javaScript of the example. For the purpose of this simple example I did not choose to use or do anything fancy, or modern. I just used the good old tired yet true XMLHttprequest to make the requests from the browser.
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.
|
|
4- The routes folder
The routes folder is a way to break things down more when making a complex express.js project that involves many different paths responding to many different types of requests. This can be done by making javaScript files in a folder that export an app or router object. What is exported by one of these files can then be used in the main app.js file, or any express javaScript file for that matter with the app.use method.
In this example I have made a file for each type of request that is used including GET, POST, and yes of course DELETE.
4.1 - The /routes/post.js file
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.
|
|
4.2 - The /routes/file.js file
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.
|
|
4.3 - The /routes/delete.js file
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.
|
|
5 The app.js file
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.
|
|
6 - Starting the app.
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.
7 - Conclusion
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.