When making an express.js project there are a few response methods that can be used to respond to a request with some kind of content. All of these methods of interest are in the standard response object that is one of the three arguments when making a function that will be used with an app method like app.get. In this post I will be writing about the response send file method for just simply sending a file that is to be displayed in the browser. This differs from other methods like the response download method that is useful for serving up a file that is to be downloaded to the client.
For a basic example of this method I made a simple script that just serves up a single *.png file. For this demo the only dependency i needed to install is just express.js itself.
Once express is installed with my demo project folder I quickly put together this demo that serves up a single png file.
By default the path to the file must be an absolute path, if I want to use a relative path then I need to give a root path with the root option. More on options including root later.
Like many of these methods there is an options object that can be given to it to set some options for the method.
If I want to use relative paths I must give the root path for the relative paths. So in other words if I just want to give a file name for the first argument then I need to set the folder that contains that file with the root option when giving an options object to send file.