"absolute path" <-- starts from "root" directory
How far back do we mean when we says an 'absolute' path goes to the 'root' directory? In the context of an ExpressJS app.. Would the "root" be the project folder correct? And if so, how does it know to stop there and consider that the 'root'. Is it looking for some identifying file or something? Like package.json?
13 Replies
root in the context of backend filesystem is
/
same for a domain actually, it's just the domain (so https://google.com/ is the root of anything below google.com)
for the filesystem, that's the linux filesystem root, so if your project lives in /var/www/html/my-cool-project/
, the index.html
's file absolute path is /var/www/html/my-cool-project/index.html
so from my app.js.. I'm using an app.get() to get my index.html. The app.js and the index.html are on the the same 'level' in the filesystem.. so the first argument of .get(), the path, is just '/'...
And then when I'm using res.sendFile(), which requires an absolute file path, I use path.resolve(), which returns an abs path.
But I'm wondering how path.resolve() knows where the 'root' is?
it's always at
/
so is it fair to say the 'root' is always the folder that the app.js is in... in other words "/" will always be whereever app.js is?
no, that's a relative path
/ is the equivalent of C:\ in windows
path.resolve produces an absolute path that starts at the root of the filesystem you're on, which on a linux machine is always
/
, the very beginning of any path and where all the files livebut where is that beginning though, are we talking about the root of the whole computer filesystem? Because even my project folder is itself inside of other folders?
"where all files live"
of the project?
the whole computer
ahhhh
absolute paths are a unique way to identify any file on your computer
gotcha okay.. so path.resolve() gives the path to the file at a 10,000ft 'whole computer' level
yes
okay great that makes more sense then, I'd been thinking it was from the 'root' of the project this whole time
thank you thank you
no worries 🙂