读取文件,然后使用node.js删除文件
我正在尝试使用nodeJS执行以下操作:
启动可执行文件(可执行文件,然后创建一个timestamp_Question.txt文件),读取创建的.txt文件,并将文本返回到我的前端。接受用户输入并使用包含用户输入的新名称创建一个新的timestamp_Answer.txt文件,然后删除原始txt文件。
除删除出现以下错误的原始文件外,其他所有内容似乎都可以正常工作
错误:EBUSY:资源忙或锁定,取消链接'c:\ projects \ pizzabox \ 95912_Question.txt”错误(本机)
以下是我的服务器代码的各个部分:
启动.exe文件:
startProgram: function(req, res){
var date = new Date().toLocaleTimeString();
date = date.replace(/\D/g,'');
exec('C:/projects/pizzabox/server/webserver/Pizza_page_server.exe', [date], function(err, data) {
console.log(err)
});
res.send(date) /// front end needs date value to track file as date is attached to created txtfile
}
前端请求由exe创建的txt的内容:
getDocument: function(req,res){
console.log('file requested', req.params.dateStamp);
var directory = 'C:/projects/pizzabox/'
var fileCode = req.params.dateStamp.toString()
var fileSuffix = "_Question.txt"
var file = directory+fileCode+fileSuffix
console.log('file is', file);
while(!fs.existsSync(file)){};
fs.readFile(file, 'utf8', function(err,data){
if (err) throw err;
console.log('read ', data);
res.send(data)
});
前端提交数据以创建新的timestamp_Answer.txt文件并删除旧的timestamp_Question.txt文件:
sendValue: function(req,res){
console.log('got value', req.body.value, " questionNumber ", req.body.questionNumber, "timestamp ", req.body.timestamp);
fs.appendFile(req.body.timestamp+'_Answer.txt', "Question_Num:"+req.body.questionNumber+"\nAnswer_Val:"+req.body.value, function(err){
if(err) throw err;
console.log('file created!');
fs.unlink(req.body.timestamp+'_Question.txt', function(err){
if (err) throw err;
console.log('question file deleted');
})
})
}
解除链接的任何原因都无法访问该文件? fs.readFile()完成后是否不会关闭/退出文件?
回答如下:看来您在这里至少有一个比赛条件。仅仅因为fs.fileExistsSync()
看到文件存在,并不意味着.exe已完成写入该文件或已关闭该文件。因此,很有可能在其他进程仍打开文件时调用fs.readFile()
。您可能想重新考虑所有操作的顺序。
首先,您现在拥有的设计是单用户设计。您的服务器一次只能由一个用户使用,因为您无法区分来自不同用户的请求。
第二,您应该转到更有针对性的顺序设计,而不是尝试使用文件系统中文件的存在作为证据,以证明现在可以进行某些先前的操作。
第三,关闭while(!fs.existsSync(file)){};
绝不用于一次性脚本中,因为它会中止服务器中任何类型的请求或计时器或任何事件的所有处理,直到该文件存在为止。此外,它可能占用了所有CPU。它从未在多用户服务器中使用过。
您并没有真正描述您要设计的总体客户端/服务器流程,也没有描述这是单用户系统还是最终打算供多个用户使用,因此我们很难就应该在何处提出建议。进行建筑重新设计。
您可能会做一个简单的改进,直到完成.exe
才将日期退还给客户,希望这将阻止它要求下一步,直到.exe完成:
startProgram: function(req, res){
var date = new Date().toLocaleTimeString();
date = date.replace(/\D/g,'');
exec('C:/projects/pizzabox/server/webserver/Pizza_page_server.exe', [date], function(err, data) {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
// don't send this until the .exe is done
res.send(date) // front end needs date value to track file as date is attached to created txtfile
}
});
}
读取文件,然后使用node.js删除文件
我正在尝试使用nodeJS执行以下操作:
启动可执行文件(可执行文件,然后创建一个timestamp_Question.txt文件),读取创建的.txt文件,并将文本返回到我的前端。接受用户输入并使用包含用户输入的新名称创建一个新的timestamp_Answer.txt文件,然后删除原始txt文件。
除删除出现以下错误的原始文件外,其他所有内容似乎都可以正常工作
错误:EBUSY:资源忙或锁定,取消链接'c:\ projects \ pizzabox \ 95912_Question.txt”错误(本机)
以下是我的服务器代码的各个部分:
启动.exe文件:
startProgram: function(req, res){
var date = new Date().toLocaleTimeString();
date = date.replace(/\D/g,'');
exec('C:/projects/pizzabox/server/webserver/Pizza_page_server.exe', [date], function(err, data) {
console.log(err)
});
res.send(date) /// front end needs date value to track file as date is attached to created txtfile
}
前端请求由exe创建的txt的内容:
getDocument: function(req,res){
console.log('file requested', req.params.dateStamp);
var directory = 'C:/projects/pizzabox/'
var fileCode = req.params.dateStamp.toString()
var fileSuffix = "_Question.txt"
var file = directory+fileCode+fileSuffix
console.log('file is', file);
while(!fs.existsSync(file)){};
fs.readFile(file, 'utf8', function(err,data){
if (err) throw err;
console.log('read ', data);
res.send(data)
});
前端提交数据以创建新的timestamp_Answer.txt文件并删除旧的timestamp_Question.txt文件:
sendValue: function(req,res){
console.log('got value', req.body.value, " questionNumber ", req.body.questionNumber, "timestamp ", req.body.timestamp);
fs.appendFile(req.body.timestamp+'_Answer.txt', "Question_Num:"+req.body.questionNumber+"\nAnswer_Val:"+req.body.value, function(err){
if(err) throw err;
console.log('file created!');
fs.unlink(req.body.timestamp+'_Question.txt', function(err){
if (err) throw err;
console.log('question file deleted');
})
})
}
解除链接的任何原因都无法访问该文件? fs.readFile()完成后是否不会关闭/退出文件?
回答如下:看来您在这里至少有一个比赛条件。仅仅因为fs.fileExistsSync()
看到文件存在,并不意味着.exe已完成写入该文件或已关闭该文件。因此,很有可能在其他进程仍打开文件时调用fs.readFile()
。您可能想重新考虑所有操作的顺序。
首先,您现在拥有的设计是单用户设计。您的服务器一次只能由一个用户使用,因为您无法区分来自不同用户的请求。
第二,您应该转到更有针对性的顺序设计,而不是尝试使用文件系统中文件的存在作为证据,以证明现在可以进行某些先前的操作。
第三,关闭while(!fs.existsSync(file)){};
绝不用于一次性脚本中,因为它会中止服务器中任何类型的请求或计时器或任何事件的所有处理,直到该文件存在为止。此外,它可能占用了所有CPU。它从未在多用户服务器中使用过。
您并没有真正描述您要设计的总体客户端/服务器流程,也没有描述这是单用户系统还是最终打算供多个用户使用,因此我们很难就应该在何处提出建议。进行建筑重新设计。
您可能会做一个简单的改进,直到完成.exe
才将日期退还给客户,希望这将阻止它要求下一步,直到.exe完成:
startProgram: function(req, res){
var date = new Date().toLocaleTimeString();
date = date.replace(/\D/g,'');
exec('C:/projects/pizzabox/server/webserver/Pizza_page_server.exe', [date], function(err, data) {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
// don't send this until the .exe is done
res.send(date) // front end needs date value to track file as date is attached to created txtfile
}
});
}