简体   繁体   English

错误:ENOENT:没有这样的文件或目录,打开尝试使用 fs 访问目录时

[英]Error: ENOENT: no such file or directory, open When trying to access a directory with fs

I am not knowledgeable in nodejs or express, I have an API running on http://localhost:3000 and one of the endpoints call a function that uses file system to read file synchronously.我对 nodejs 或 express 不了解,我有一个在 http://localhost:3000 上运行的 API,并且其中一个端点调用了一个使用文件系统同步读取文件的函数。 When I make a post request on postman, it says in the console that it can't read a path that doesn't exist (which does to my understanding).当我在邮递员上发出发布请求时,它在控制台中说它无法读取不存在的路径(据我了解)。

Relevant code:相关代码:

index.js index.js

app.post('/write',(req,res)=>
{
    var body = req.body;
    console.log('endpoint reached');
    console.log(body);
    logic.append(body.key,body.path);
    res.send('Writting to state was successful');
});

stateLogic.js (this occurs on first initialization of trieRoot) stateLogic.js(这发生在第一次初始化 trieRoot 时)

async append(key,path)
    {
        var trieRoot = Buffer.from(programData.volatile.modularVariables.readSync(programData.persistent.paths.srcRoot,programData.volatile.modularVariables.encoding.utf8),'hex');

        console.log(trieRoot);

        var db = programData.persistent.states.singularity;
        var trie = new merkle_patricia_tree_1.SecureTrie(db,trieRoot);

        var data = programData.volatile.modularVariables.readSync(path,programData.volatile.modularVariables.encoding.utf8);

        var format = programData.volatile.modularVariables.getFormat(path);

        var name = programData.volatile.modularVariables.getName(path);

        var inData = JSON.stringify({name,format,data});
        

        console.log(`key: ${key} value: ${inData}`);

        await trie.put(Buffer.from(key),Buffer.from(inData));  

        var root = await trie.root; 

        programData.volatile.modularVariables.write(programData.persistent.paths.srcRoot,root.toString('hex'),programData.volatile.modularVariables.writeCB);

        var retGet = await trie.get(Buffer.from(key));

        console.log('Get returned:'+retGet.toString());
        console.log(`From Stack: ${root.toString('hex')} From File: ${this.malleableVar}`);

        return;
    };

readSync function used:使用的 readSync 函数:

readSync: (file,encoding)=>{return fs.readFileSync(file,{encoding:encoding,flag:'r'})},

srcRoot value used:使用的 srcRoot 值:

srcRoot: './storage/root.txt'

Console Error:控制台错误:

(node:18808) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open './storage/root.txt'

Paths:路径: 来自 index.js 的 stateLogic.js 路径,以及来自 stateLogic.js 的 root.txt 路径

Questions:问题:

Why does it say the path doesn't exist when it does?为什么它说路径不存在? And what is it I am doing wrong?我做错了什么? Thank you for your time.感谢您的时间。

You've to use absolute path instead of relative path您必须使用absolute path而不是relative path

Modify your index.js with the below code:使用以下代码修改您的index.js

const path = require('path') // <-- import path module to use absolute path.

app.post('/write',(req,res)=>
{
    var body = req.body;
    const absPath = path.join(__dirname, body.path); // <-- absolute path
    console.log("Absolute Path: ", absPath);
    logic.append(body.key, absPath);
    res.send('Writting to state was successful');
});

Note: If you're still facing the same error, then check body.path value.注意:如果您仍然面临同样的错误,请检查body.path值。

we can use path module to provide absolute path for the directory我们可以使用路径模块为目录提供绝对路径

folder
  |-server.js
  |-cert.pem

Inside server.js file在 server.js 文件中

const const path = require('path') 
const certpath = path.join(__dirname,'chain.pem'); // folder\chain.pem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:ENOENT:没有那个文件或目录,打开 - Error: ENOENT: no such file or directory, open 为什么 fs.createWriteStream 抛出“错误:ENOENT:没有这样的文件或目录”错误? - Why is fs.createWriteStream throwing a "Error: ENOENT: no such file or directory" error? fs.createReadStream 错误; uncaughtException: ENOENT: 没有这样的文件或目录 - fs.createReadStream error; uncaughtException: ENOENT: no such file or directory ENOENT:运行fs.readFile时没有此类文件或目录 - ENOENT: no such file or directory when running fs.readFile Heroku错误:ENOENT:没有此类文件或目录,请打开“ .env” - Heroku Error: ENOENT: no such file or directory, open '.env' 详细堆栈错误:ENOENT:没有这样的文件或目录,打开 - verbose stack Error: ENOENT: no such file or directory, open ENOENT:没有这样的文件或目录使用fs写入文件 - ENOENT: no such file or directory writing file with fs 错误:ENOENT:没有这样的文件或目录 - Error: ENOENT: no such file or directory 错误:否:没有此类文件或目录,请在Object.fs.openSync(fs.js:646:18)中打开“ /etc/letsencrypt/live/awiclass.monoame.com/privkey.pem” - Error: ENOENT: no such file or directory, open '/etc/letsencrypt/live/awiclass.monoame.com/privkey.pem' at Object.fs.openSync (fs.js:646:18) 错误错误:ENOENT:没有这样的文件或目录,在节点js中创建文本文件时打开 - ERROR Error: ENOENT: no such file or directory, open when create a text file in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM