2016-01-13 123 views
1

我有下面的代码:fs.open失败,文件扩展名 - Node.js的

fs.open("uploads/test.txt", "a", "0755", function(err, fd){ 
    if(err) { console.log(err); } 
    else { 
    file.handler = fd; //We store the file handler so we can write to it later 
    ... 
    } 
}); 

文件被创建并写入完美时,我只是有"uploads/test",但是当我尝试做"uploads/test.txt"它打破了。有任何想法吗?

回答

0

这是真的很傻,但我发现是什么原因导致我的代码破解:

fs.open工作原理意。这个错误与我使用nodemon进行文件检测设置有关。

原因是每次我的应用程序会加载它会运行上述代码。然后代码将写入我的应用程序/uploads目录中的新文件。然后,Nodemon会检测到新文件并重新启动应用程序,从而创建一个恶性循环。

0

我想你应该尝试使用

var path = './uploads/test.txt'. 

或者

var path = __dirname + 'your_path'; 

fs.open(path, "a", "0755", function(err, fd){ 
    if(err) { console.log(err); } 
    else { 
    file.handler = fd; //We store the file handler so we can write to it later 
    ... 
    } 
});