节点js / Express js相对路径(dot或__dirname或没有任何前缀)? [英] Node js/Express js relative paths (dot or __dirname or without any prefix)?

查看:123
本文介绍了节点js / Express js相对路径(dot或__dirname或没有任何前缀)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看Nodejs / expressjs,看过不同的教程使用 __ diranme +/ my_folder./ my_folder / code>,或只是my_folder

I'm looking at Nodejs/expressjs and have seen different tutorials use either __diranme + "/my_folder", "./my_folder", or just "my_folder".

示例:

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?

我也看过 require('./ my_file.js') require('my_file')。有差异吗我应该使用什么?

I've also seen require('./my_file.js') and require('my_file'). Is there a difference? What should I use?

推荐答案

几乎任何功能(除了 )将文件路径作为参数,在某种程度上将使用 fs 模块中的函数来读取或写入它们。

Almost any function (except for require) which takes file paths as an argument, will at one point use functions from the fs module to read from or write to them.

fs模块的Node.js文档说: p>

Node.js documentation for fs module says:


可以使用文件名的相对路径,但请记住,此路径将相对于process.cwd()。

Relative path to filename can be used, remember however that this path will be relative to process.cwd().

当你想到这件事时,这些功能需要很大的诡计才能有所不同。毕竟,fs函数是常规Javascript,他们没有访问有关调用者的信息。他们可以访问的唯一 __ dirname 将是自己的模块(核心fs模块)的 __ dirname

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 __dirname they could access would be the __dirname of their own module (the core fs module).

事实上,需要函数可以解析相对于当前 __ dirname ,没有明确指定,是因为需要是每个单独的文件出现的唯一功能。这样,它可以访问当前模块的具体细节,特别是其路径。

The fact that the require function can resolve paths relative to the current __dirname, without specifying this explicitly, is because require is 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.

您的代码发生工作的原因是目前, app.js (或类似的)上面的代码恰好与当前的 process.cwd()在同一个目录中。即启动具有节点app.js 的应用程序可以正常工作,而使用节点启动该应用程序节点myappdir / app.js (run从它的父目录)不会。 process.cwd()将不同。

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.js would work, while starting the app with node myappdir/app.js (ran from its parent directory) would not. process.cwd() would be different.

只要你记住相对路径将被解决通过 process.cwd(),那么你可以使用较短的语法。在某些情况下,这可能是一个优势。它确实使你的代码依赖于它的名字。我个人更喜欢使用 __ dirname ,因为它对于发生了什么更加透明,与中使用的路径一致的相对路径需要同一个文件的语句。

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 require statement for the same file.

这篇关于节点js / Express js相对路径(dot或__dirname或没有任何前缀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆