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 as an attachment to the local file system. If I want to make a path where the user will be prompted to download a binary for there operating system I would want to use res.download, but if I just want to display an image file as content to be displayed for a path then I would want to use res.sendFile.
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.
So that is it for now when it comes to the sendFile response method. When it comes to working on some actual express projects of one kind or another I can not say that I use the send file that often thus far though. There are often other options for sending static content to be displayed in the browser, such as the express static built in middle ware, and using the serve index middleware package also with it as a way to create an index of content for a path.
The send file method however can be used it situations where some additional logic can be used to preform things like check user agent headers to send a file as content depending on factors such as the state of a user agent header, or if a user is currently logged in for example. SO then the send file does have its use case examples here and there when I do in fact get into situations in which I would want to use a method such as this.