Error reading file from disk: Error: ENOENT: no such file or directory, open './database.json

Error reading file from disk: Error: ENOENT: no such file or directory, open './database.json

我正在做一些测试,其中我正在测试 POST 路由,我认为测试写得很好,但是现在我收到一个错误

“从磁盘读取文件时出错:错误:ENOENT:没有这样的文件或目录,打开'./database.json”

describe ("POST /post methods",() => {
  it("should get /post ",(done)=>{
     const incomingRequest = {
        name: "charanjit",
        content: "im posting",
        gif: ""

     };
    chai.request(server)
    .post("/posts")
    .send(incomingRequest)
    .end(function (err, res) {
      expect(res).to.have.status(500)
      done()
    });


    
    
    


})
it("should get /comments ",(done)=>{
    const commente = {
       comment: "hi omg"

    };
    chai.request(server)
    .post("/comments")
    .send(commente)
    .end(function (err, res) {
      expect(res).to.have.status(200)
      done()
    });


 })
   it("should get /emoji ",(done)=>{
    const commente = {
    comment: "hi omg"

 };
 chai.request(server)
 .post("/comments")
 .send(commente)
 .end(function (err, res) {
  expect(res).to.have.status(200)
  done()
  });

这些是我写的测试。 这就是我在服务器端代码中写的。

 server.post("/posts", (req, res) => {
  const incomingRequest = req.body;
  if (isValidPost(incomingRequest)) {
    const post = {
   name: incomingRequest.name.toString(),
   content: incomingRequest.content.toString(),
   giph: incomingRequest.gif.toString(),
   date: new Date(),
   likes: 0,
   dislikes: 0,
   laughs: 0,
   comments: [],
  };

  //read the file
  fs.readFile("./database.json", "utf8", (err, data) => {
  if (err) {
    console.log(`Error reading file from disk: ${err}`);
  } else {
    //parse JSON string to JSON object
    const postsData = JSON.parse(data);
    post.id = postsData.length + 1;
    postsData.push(post);

    fs.writeFile(
      "./database.json",
      JSON.stringify(postsData, null, 4),
      (err) => {
        if (err) {
          console.log(`Error writing file: ${err}`);
        }
      }
    );
    res.status(201).send(post);
    }
  });
 }    else {
  res.status(422).send("Name and content required!");
 }
});

  //POST ROUTE - EMOJIS

  server.post("/emojis", (req, res) => {
 const incomingRequest = req.body;
 const emoji = incomingRequest.emoji;
 const id = incomingRequest.id;

 fs.readFile("./database.json", "utf8", (err, data) => {
 if (err) {
   console.log(`Error reading file from disk: ${err}`);
 } else {
  const postsData = JSON.parse(data);
  if (emoji === "fa-thumbs-up") {
    postsData[id - 1].likes++;
  }
  if (emoji === "fa-thumbs-down") {
    postsData[id - 1].dislikes++;
  }
  if (emoji === "fa-laugh-squint") {
    postsData[id - 1].laughs++;
  }

  fs.writeFile(
    "./database.json",
    JSON.stringify(postsData, null, 4),
    (err) => {
      if (err) {
        console.log(`Error writing file: ${err}`);
      }
    }
    );
  }
   res.status(201).json([emoji, id]);
  });
 });

  server.post("/comments", (req, res) => {
  const incomingRequest = req.body;
  const comment = incomingRequest.comment;
  const id = incomingRequest.id;

fs.readFile("./database.json", "utf8", (err, data) => {
 if (err) {
   console.log(`Error reading file from disk: ${err}`);
  } else {
  const postsData = JSON.parse(data);
  postsData[id - 1].comments.push(comment);
  fs.writeFile(
    "./database.json",
    JSON.stringify(postsData, null, 4),
    (err) => {
      if (err) {
        console.log(`Error writing file: ${err}`);
        }
      }
     );
   }
   res.status(201).send(comment);
    });
  });

是什么导致了这个错误,我是否需要编写一个访问数据库的测试,我有点困惑,我该怎么做?

项目结构文件: enter image description here

根据您的项目结构,您只需向上移动一个文件夹,然后转到 server 文件夹。

此代码有效:

fs.readFile("../server/database.json", "utf8", (err, data) => {
  // ...
}

请注意,您必须使用 ../ 才能转到父目录。然后你可以到达 server/ 中名为 database.json.

的文档