Node js/Express js 相对路径(点或 __dirname 或没有任何前缀)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16727045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node js/Express js relative paths (dot or __dirname or without any prefix)?
提问by Raekye
I'm looking at Nodejs/expressjs and have seen different tutorials use either __diranme + "/my_folder", "./my_folder", or just "my_folder".
我在看的NodeJS / expressjs和看到不同的教程请使用__diranme + "/my_folder","./my_folder"或只"my_folder"。
Examples:
例子:
app.use("/static", express.static(__dirname + "/my_folder"));
app.use("/static", express.static("./my_folder"));
app.use("/static", express.static("my_folder"));
I tried them all and they all seem to work; which one should I use for relative paths?
我尝试了所有方法,它们似乎都有效;我应该将哪一个用于相对路径?
I've also seen require('./my_file.js')and require('my_file'). Is there a difference? What should I use?
我也见过require('./my_file.js')和require('my_file')。有区别吗?我应该使用什么?
采纳答案by Myrne Stol
Almost any function (except for require) which takes file paths as an argument, will at one point use functions from the fsmodule to read from or write to them.
几乎所有require将文件路径作为参数的函数(除了),都会在某一时刻使用fs模块中的函数来读取或写入它们。
Node.js documentation for fs modulesays:
Relative path to filename can be used, remember however that this path will be relative to process.cwd().
可以使用文件名的相对路径,但请记住,此路径将相对于 process.cwd()。
When you think about it, it would require major trickery to have these functions behave any differently. After all, the fs functions are regular Javascript and they don't have special access to information about the caller. The only __dirnamethey could access would be the __dirnameof their own module (the core fs module).
仔细想想,要让这些函数的行为有任何不同,就需要很大的技巧。毕竟, fs 函数是常规的 Javascript,它们没有对调用者信息的特殊访问权限。__dirname他们唯一可以访问的__dirname是他们自己的模块(核心 fs 模块)的。
The fact that the requirefunction canresolve paths relative to the current __dirname, without specifying this explicitly, is because requireis a unique function for every single file it appears in. This way, it has access to the current module's specifics, and in particular its path.
该require函数可以解析相对于当前 的路径__dirname,而无需明确指定这一事实,这是因为require它出现在每个文件中的唯一函数。通过这种方式,它可以访问当前模块的细节,特别是它的路径。
The reason your code happens to work is that currently, the app.js(or similar) the code above appears in happens to be in the same directory as what process.cwd()currently is. I.e. starting the app with node app.jswould work, while starting the app with node myappdir/app.js(ran from its parent directory) would not. process.cwd()would be different.
您的代码碰巧起作用的原因是,目前,app.js上面出现的(或类似的)代码恰好与process.cwd()当前所在的目录位于同一目录中。即启动应用程序node app.js会起作用,而启动应用程序node myappdir/app.js(从其父目录运行)则不会。process.cwd()会有所不同。
As long as you keep in mind that relative paths will be resolved via process.cwd(), then you could use the shorter syntax. In some cases it can be an advantage. It does make your code dependent on how it's called though. I personally prefer using __dirname, because it's somewhat more transparent as to what's happening, and the relative paths consistent with the paths you use in a requirestatement for the same file.
只要您记住相对路径将通过 解析process.cwd(),那么您就可以使用较短的语法。在某些情况下,这可能是一个优势。它确实使您的代码依赖于它的调用方式。我个人更喜欢使用__dirname,因为它对发生的事情更加透明,并且相对路径与您在require同一文件的语句中使用的路径一致。
回答by Peter Lyons
The __dirnameversion is the most robust since __dirnamewill always be the directory containing the currently executing .jsfile, which is a better anchor than "my_folder"or "./my_folder"which are both relative paths and will fail if the process's current working directory (process.cwd()) is something unexpected, which is entirely possible.
该__dirname版本是最健壮的,因为__dirname它将始终是包含当前正在执行的.js文件的目录,这比"my_folder"或"./my_folder"两者都是相对路径更好,并且如果进程的当前工作目录 ( process.cwd()) 出现意外情况,则将失败,这是完全可能的。
Note that it's a different story for paths passed to require, since relative paths there are resolved relative to the location of the calling module without regard for the process's current working directory (again, this makes them less fragile).
请注意,传递到 的路径是另一回事require,因为相对路径是相对于调用模块的位置解析的,而不考虑进程的当前工作目录(同样,这使它们不那么脆弱)。
回答by Four_lo
I use app.use(express.static(path.join(__dirname, 'public')));which i then have three folders inside of my public, stylesheets, javascripts and images which allows me to access those static files by
我使用app.use(express.static(path.join(__dirname, 'public')));它,然后在我的公共、样式表、javascripts 和图像中有三个文件夹,它们允许我通过以下方式访问这些静态文件
<link rel="stylesheet" href="/stylesheets/jquery-ui.css" />
<script type="text/javascript" src="/javascripts/jquery-1.9.1.js"></script>
the ./myfolderapproach I believe is essentially the same thing. I have never used it.
the /myfolderapproach I have never seen.
./myfolder我相信的方法本质上是一样的。我从未使用过它。我从未见过
的/myfolder方法。
require('./my_file.js') I use as
require('./routes/my_file.js') aand is where I store all my express routes.
The require('myfile')is a call to packages that you install or come install with express.
这require('myfile')是对您安装或随 express 安装的软件包的调用。

