My app structure is:
/app /css /js .. /script /server.js
I'm trying to use __dirname to point to /app folder when using
app.use(express.static( __dirname + '/app'));
I dont realy know what to search for in the web, please help.
__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
__dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.
To go back 1 folder level with __dirname in Node. js, we can use the path. join method. const reqPath = path.
The __dirname in a node script returns the path of the folder where the current JavaScript file resides. __filename and __dirname are used to get the filename and directory name of the currently executing file. The ./ gives the current working directory. It works similar to process.
You can use path module for that.
The path module provides utilities for working with file and directory paths.
const path = require('path'); app.use(express.static(path.join(__dirname, '..', 'app')));
if you're in server.js then you mean
app.use(express.static( __dirname + '/../app'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With