Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go up using __dirname in the folder hierarchy

Tags:

express

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.

like image 531
Tzelon Avatar asked Aug 06 '13 18:08

Tzelon


People also ask

What is __ Dirname in path?

__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.

What does __ Dirname mean in node?

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

How do I go back in dirname?

To go back 1 folder level with __dirname in Node. js, we can use the path. join method. const reqPath = path.

How do I use dirname in node JS?

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.


2 Answers

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'))); 
like image 42
Vlad Bezden Avatar answered Sep 29 '22 18:09

Vlad Bezden


if you're in server.js then you mean

app.use(express.static( __dirname + '/../app')); 
like image 63
Joe Minichino Avatar answered Sep 29 '22 17:09

Joe Minichino