节点从 %appdata% 读取文件
Node Read File From %appdata%
我是 运行ning 节点,我想从 %appdata% 文件夹中读取一个文件,我不想对该路径进行硬编码。
这基本上就是我目前所拥有的:
//...require(some things)
var fs = require('fs');
var fileData;
try{
fileData = fs.readFileSync('%appdata%/folder/file.txt',{encoding:'utf8'});
}
catch(e){
console.log(e);
fileData = 42; //default value
}
//... app.get(some things)
//... app.listen
当我 运行 这样做时,我收到消息:
{ [Error: ENOENT, no such file or directory 'C:\projectdirectory\%appdata%\folder\file.txt']
errno: -4058,
code: 'ENOENT',
path: 'C:\projectdirectory\%appdata%\folder\file.txt',
syscall: 'open' }
如何让它识别 %appdata% 变量?
您必须改为从 process.env
获取值:
fileData = fs.readFileSync(process.env.APPDATA + '/folder/file.txt',{encoding:'utf8'});
我是 运行ning 节点,我想从 %appdata% 文件夹中读取一个文件,我不想对该路径进行硬编码。
这基本上就是我目前所拥有的:
//...require(some things)
var fs = require('fs');
var fileData;
try{
fileData = fs.readFileSync('%appdata%/folder/file.txt',{encoding:'utf8'});
}
catch(e){
console.log(e);
fileData = 42; //default value
}
//... app.get(some things)
//... app.listen
当我 运行 这样做时,我收到消息:
{ [Error: ENOENT, no such file or directory 'C:\projectdirectory\%appdata%\folder\file.txt']
errno: -4058,
code: 'ENOENT',
path: 'C:\projectdirectory\%appdata%\folder\file.txt',
syscall: 'open' }
如何让它识别 %appdata% 变量?
您必须改为从 process.env
获取值:
fileData = fs.readFileSync(process.env.APPDATA + '/folder/file.txt',{encoding:'utf8'});