I wonder if the following two are the same.
(1) app.use(express.static(__dirname + "/public"));
(2) app.use(express.static("public"));
Because I think as long as (2)
exists in the express server, the browser can serve up the public
folder which is located in the root path and __dirname
isn't required.
However, sometimes (2)
doesn't work while (1)
works such as if I render a ejs file in the dynamic route based on Route parameters.
What exactly is the difference between them?
first of all __dirname have 3 usage, you can check documentation:
Making New Directories
Pointing to Directories
Adding Files to a Directory
based on your example we can don't use __dirname but if you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve like this