JS node 怎么实现循环判断
主要做的是图片上传存储的功能 存储路径为pic/tname 如果存在 就分离图片名字 前后缀比如a.jpg 分离后
重新起名 a(1).jpg,然后再进行fs.exist 判断,直到不存在 问题时在重新命名后我要怎么返回fs.exists进行判断 形成循环 求指点
fs.exists(url,function(exists){
if(exists){
console.log('exist');
var fileType=/\.[^\.]+/.exec(tname);
var b=0;b++;
var p=tname.lastIndexOf('.');
var fileName = tname.substr(0,p);
tname = fileName +'('+b+')'+fileType;
url = 'pic/'+tname;
}
else{
console.log('not exist');
fs.mkdir('pic',function(err){
fs.writeFile('pic'+'/'+tname,data,function(err){
res.send(result);
console.log('succeed');
});
})
}
});