如何用nodejs实现一定时间的文件监控
1个回答
展开全部
function watchResponseFile(filePath, num, response) {
if (num < 10) {
fs.exists(filePath, function (exists) {
if (exists) {
fs.readFile(filePath, "utf-8", function (err, data) {
if (!err) {
response.writeHead(200, { "Content-Type": "text/json" });
response.write(data);
response.end();
}
else {
response.writeHead(200, { "Content-Type": "text/json" });
response.write("fail:" + err);
response.end();
}
//删除响应文件
fs.unlink(filePath, function (err) {
});
});
}
else {
num = num + 1;
setTimeout(watchResponseFile(filePath, num, response), 10000);
}
});
}
if (num < 10) {
fs.exists(filePath, function (exists) {
if (exists) {
fs.readFile(filePath, "utf-8", function (err, data) {
if (!err) {
response.writeHead(200, { "Content-Type": "text/json" });
response.write(data);
response.end();
}
else {
response.writeHead(200, { "Content-Type": "text/json" });
response.write("fail:" + err);
response.end();
}
//删除响应文件
fs.unlink(filePath, function (err) {
});
});
}
else {
num = num + 1;
setTimeout(watchResponseFile(filePath, num, response), 10000);
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询