Serving Static Files With express.static

By Hemanta Sundaray on 2021-11-09

express.static is a built-in middleware function used to serve static files such as images, CSS files and JavaScript files.

The function signature is:

express.static(root)
  • root is the directory from which we want to serve our static assets.

The path that we provide to the express.static function is relative to the directory from which we launch our node process. So, it’s safer to use the absolute path of the directory that we want to serve.

if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "../client/build")))
}

In the code example above, we have passed __dirname as the first argument and the directory that contains static files as the second argument in path.join().

__dirname is an environment variable that tells us the absolute path of the directory containing the currently executing file.

Join the Newsletter