expressHandling static files


Syntax

  1. To serve static files (Images, CSS, JS files, etc.) use the express.static middleware function.
  2. Pass the name of the directory that contains the assets to express.static to serve the files directly. (Look to the Basic Example)
  3. You can use multiple directories, simply call the express.static multiple times. Remember, Express looks up files in the order you set the directories with express.static. (Look to the Multiple Directories Example)
  4. You can create a virtual path prefix (i.e. one where the path does not actually exist in the file system) with express.static, just specify a mount path. (Look to the Virtual Path Prefix Example)
  5. All of the preceding paths have been relative to the directory from where you launch the node process. So, it is generally safer to use the absolute path of the directory you want to serve. (Look to the Absolute Path to Static Files Directory Example)
  6. You can mix and match the options of this method, as seen in the Absolute Path to Directory & Virtual Path Prefix Example

Remarks

All of the examples can be run in node. Simply copy and paste into a node project with Express installed and run them with node filename. For an example of how to install express click here and ensure you have npm installed then follow the instructions on installing packages to install "express."