fs模块函数使用的当前目录是什么?

时间:2021-12-12 23:18:09

What is the current directory when a method in the fs module is invoked in a typical node.js/Express app? For example:

在典型的node.js / Express应用程序中调用fs模块中的方法时,当前目录是什么?例如:

var fs = require('fs');
var data = fs.readFile("abc.jpg", "binary");

What is the default folder used to locate abc.jpg? Is it dependent on the containing script's folder location?

用于定位abc.jpg的默认文件夹是什么?它是否依赖于包含脚本的文件夹位置?

Is there a method to query the current directory?

有查询当前目录的方法吗?


My file structure is:

我的文件结构是:

ExpressApp1/
           app.js
           routes/
                 members.js

In members.js I have a fs.createWriteStream("abc") and file abc was created in ExpressApp1/

在members.js中我有一个fs.createWriteStream(“abc”),文件abc是在ExpressApp1 /中创建的

1 个解决方案

#1


17  

It's the directory where the node interpreter was started from (aka the current working directory), not the directory of script's folder location (thank you robertklep).

它是启动节点解释器的目录(也就是当前工作目录),而不是脚本文件夹位置的目录(谢谢robertklep)。

Also, current working directory can be obtained by:

此外,当前工作目录可以通过以下方式获得:

process.cwd()

For more information, you can check out: https://nodejs.org/api/fs.html

有关更多信息,请查看:https://nodejs.org/api/fs.html

EDIT: Because you started app.js by node app.js at ExpressApp1 directory, every relative path will be relative to "ExpressApp1" folder, that's why fs.createWriteStream("abc") will create a write stream to write to a file in ExpressApp1/abc. If you want to write to ExpressApp1/routes/abc, you can change the code in members.js to:

编辑:因为您在ExpressApp1目录中通过节点app.js启动了app.js,所以每个相对路径都相对于“ExpressApp1”文件夹,这就是为什么fs.createWriteStream(“abc”)将创建写入流以写入文件的原因。 ExpressApp1 / ABC。如果要写入ExpressApp1 / routes / abc,可以将members.js中的代码更改为:

fs.createWriteStream(path.join(__dirname, "abc"));

For more:

更多:

https://nodejs.org/docs/latest/api/globals.html#globals_dirname

https://nodejs.org/docs/latest/api/globals.html#globals_dirname

https://nodejs.org/docs/latest/api/path.html#path_path_join_paths

https://nodejs.org/docs/latest/api/path.html#path_path_join_paths

#1


17  

It's the directory where the node interpreter was started from (aka the current working directory), not the directory of script's folder location (thank you robertklep).

它是启动节点解释器的目录(也就是当前工作目录),而不是脚本文件夹位置的目录(谢谢robertklep)。

Also, current working directory can be obtained by:

此外,当前工作目录可以通过以下方式获得:

process.cwd()

For more information, you can check out: https://nodejs.org/api/fs.html

有关更多信息,请查看:https://nodejs.org/api/fs.html

EDIT: Because you started app.js by node app.js at ExpressApp1 directory, every relative path will be relative to "ExpressApp1" folder, that's why fs.createWriteStream("abc") will create a write stream to write to a file in ExpressApp1/abc. If you want to write to ExpressApp1/routes/abc, you can change the code in members.js to:

编辑:因为您在ExpressApp1目录中通过节点app.js启动了app.js,所以每个相对路径都相对于“ExpressApp1”文件夹,这就是为什么fs.createWriteStream(“abc”)将创建写入流以写入文件的原因。 ExpressApp1 / ABC。如果要写入ExpressApp1 / routes / abc,可以将members.js中的代码更改为:

fs.createWriteStream(path.join(__dirname, "abc"));

For more:

更多:

https://nodejs.org/docs/latest/api/globals.html#globals_dirname

https://nodejs.org/docs/latest/api/globals.html#globals_dirname

https://nodejs.org/docs/latest/api/path.html#path_path_join_paths

https://nodejs.org/docs/latest/api/path.html#path_path_join_paths