I have my server.js
file at let's say directory: /dir1
I start the server with node server.js
.
I have my file routes.js
in directory /dir1/app/
.
I want to get the directory of the server.js
file.
I am not too sure how to do this. I can place code that tells me the location of the current script. But what is the current script?
A web app makes use of multiple files. So how do i get the dir of server.js
?
EDIT: Here is a code example:
// routes.js
const spawn = require('child_process').spawn;
const fs = require('fs');
module.exports = function(app)
{
//the file upload button does a POST the the '/' route
//i left the arrow function notation for reference
app.post('/', (req, res) => {
if (req.files)
{
var pathAndFileName = '/home/user1/Desktop/app/dataLocation/'+fileName;
As you can see, pathAndFileName is hardcoded. It works on my system, but it does not for every system.
So i would like to be to get the location of the server.js
file (in /dir1
), inside the app.js
file, so i can the append to the directory the '/dataLocation' + fileName
.
If routes.js
is in:
/home/user1/Desktop/app/dir1
And, server.js
is in:
/home/user1/Desktop/app
And, then the file you're trying to get to is in:
/home/user1/Desktop/app/dataLocation
Then, from routes.js, you can get the directory of server.js with this:
const serverjsDir = path.join(__dirname, ".."); // go up one level
Or, you could get the whole path your edit seems to be looking for (in a dataLocation
subdirectory below where server.js
is) with this:
const pathAndFileName = path.join(__dirname, '../dataLocation', fileName).
More detail
If you're using CommonJS modules (that use require()
), then you can get the current script's directory with __dirname
and then it's up to you to know where other resources are relative to your own directory so you can navigate up or down the directory hierarchy from __dirname
. For example, if you want to get access to a file main.css
in another directory at the same directory level as your script file, you could construct at path like this:
let filePath = path.join(__dirname, "../css/main.css");
But what is the current script?
The current script is the script that contains the code that is accessing __dirname
.
A web app makes use of multiple files. Since the place where i want to embed the location (in its code) is located inside routes.js, i guess this is where is should target, am i correct?
It's not clear to me exactly what you mean here. If you can provide a specific example showing what code located in what directory wants to access what file located in what other directory, then we could offer you the precise code to get to that file.
If the code that wants to access this other file is in routes.js
, then the location of routes.js
would be in __dirname
and you would build a relative path from that location as shown above.
To help you understand, __dirname
is not a global variable. It's a module-specific variable that contains a different value for every single module location in your project. So, whatever code is running in whatever module will have access to it's own copy of __dirname
that specifies the location of that specific code.