87,955
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
//监控响应文件
fs.watchFile('d:/out_recv/' + guid + ".rsp", function (curr, prev) {
if (curr.mtime.getTime() !== prev.mtime.getTime()) {
isrsp = true;
fs.unwatchFile('d:/out_recv/' + guid + ".rsp");
response.writeHead(200, { "Content-Type": "text/json" });
fs.readFile('d:/out_recv/' + guid + ".rsp", "utf-8", function (err, data) {
if (!err) {
response.write(data);
response.end();
}
else {
response.write("fail:" + err);
response.end();
}
//删除响应文件
fs.unlink('d:/out_recv/' + guid + ".rsp", function (err) {
if (!err)
console.log('删除响应文件: ' + guid + ".rsq");
else
console.log('删除响应文件出错: ' + err);
});
});
}
});
//创建计时器,30S后停止监控响应文件
setTimeout(function () {
if (!isrsp) {
fs.unwatchFile('d:/out_recv/' + guid + ".rsp");
response.writeHead(200, { "Content-Type": "text/plan" });
response.write("fail:no response json file");
response.end();
}
}, 30000);
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);
}
});
}
else {
response.writeHead(200, { "Content-Type": "text/json" });
response.write("fail:no response json file");
response.end();
}
}