简体   繁体   English

Node.JS:fs.readFile 不适用于 Ubuntu

[英]Node.JS: fs.readFile not working on Ubuntu

I'm trying to read some xml files, and when I test the code in my computer (OS X) it's working really good, without problems, but when I test the code in a Ubuntu 16 Server in DigitalOcean, I get nothing... I'm using fs.watch() and yes, it has nothing to be with the route because the program knows theres a new file in the directory but when I try to read the console.log prints nothing.我正在尝试读取一些 xml 文件,当我在我的计算机 (OS X) 中测试代码时,它运行得非常好,没有问题,但是当我在 DigitalOcean 的 Ubuntu 16 服务器中测试代码时,我什么也没得到。 . 我正在使用fs.watch() ,是的,它与路由无关,因为程序知道目录中有一个新文件,但是当我尝试读取 console.log 时,它什么也没打印。 I've already tried with readFile() and readFileSync() ... I'm sending the files to the directory using scp我已经尝试过readFile()readFileSync() ......我正在使用scp将文件发送到目录

I'm getting the eventType logs when I catch a file into the directory and the console.log("filename: ",filename) is giving me the name of the file, so, that's why I suggest the fs.readFileSync() is the problem当我将文件捕获到目录中时,我正在获取eventType日志并且console.log("filename: ",filename)给了我文件的名称,所以,这就是为什么我建议fs.readFileSync()是问题

Any suggestion?有什么建议吗?

    let dir = __dirname + '/../files/'
    fs.watch(dir, 'utf8',(eventType, filename) => {
      console.log("eventType: ", eventType)
      if(eventType === 'rename' && filename) {
        console.log("filename: ",filename)
        fs.readFile(`${dir}${filename}`, 'utf8', (err, data) => {
          if(err) console.log("error: ", err)
          console.log(data)
        }) 
   //  let data = fs.readFileSync(`${dir}${filename}`, 'utf8')
   //  console.log("DATA: ", data)<-- as I said, I've already tried with both, readFile() and readFileSync()
      }
    });

I had the same problem with path.resolve() and readFile() methods.我对path.resolve()readFile()方法有同样的问题。 To fix this issue, I used resolve-path and parseurl libraries.为了解决这个问题,我使用了resolve-pathparseurl库。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM