so In my get route for express:
app.get('/', async (req: any, res: any) => {
const images:Array<image> = readFiles()
try {
console.log(images)
res.render('index',{images})
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
I load the images array, which updates every time. But because it's called every time, it duplicates each image. How can I change the readfiles function below so that it verifies whether image already exists:
interface image {
name: Promise<string>;
id: string ;
realname: string
}
const images:image[] = []
const directoryPath = path.join(__dirname,'files')
function readFiles(){
fs.readdir(directoryPath,function readdir(err:string,files:any){
if(err){
console.log("Error reading file",err)
return
}
files.forEach(function forFile(file:any){
console.log(file)
let newimage: image = {
name:getSHA256Hash(file),id:randomUUID(),realname:file
}
images.push(newimage)
})
})
return images
}