catch error in express route is not working

I am working in node js to create API it’s function as follow
This API will be used to send images from Images folder then store the image name which has been sent to the images_thumb folder
after resizing the image with Sharp
if the server got request for any image which has been sent before the API shall send the image from Images_thumb folder
—-Proplem
When ever I open the browser to http://localhost:3000/?name=6 (6 is an image name not exists in the images folder for example)
the image name is added to visited array and i donot know how to send res.send(“file not found”)

const express = require("express");
const sharp = require("sharp");


const app = express();
let visited = []; // This array is used to sore file names for comparison later
let found; // Boolean to be true if the file name exists in Visited Array

 app.get("/", (req, res) => {
 if (visited.length == 0 ) { // is the server just started 
console.log(typeof res.statusCode);
sharp(__dirname + "\images\" + req.query.name + ".jpg") // sharp is used to resize the image 
 and save the resized version in the Images_thumb folder
    .resize(200,200)
    .toFile(__dirname + "\images_thumb\" + req.query.name + ".jpg")
    .then(console.log("done")).catch((err)=>{
      res.send('Not found');
      console.log(err);
    })
res.sendFile(__dirname + "\images\" + req.query.name + ".jpg");
visited.push(req.query.name);
console.log("initial length 0");

} else if (visited.length > 0) {
for (let index = 0; index <= visited.length; index++) { // used to chek if file name existis
in the array Visited
const element = visited[index];
console.log(index + “loop”);
if (element === req.query.name && res.statusCode==200) {

    res.sendFile(__dirname + "\images_thumb\" + req.query.name + ".jpg");
    console.log(index + "break");
    found = true;
    //res.send(index+"break")
    break;
  } else {
   
    console.log("false");
    found = false;
  }
}

} else {
}

if (visited.length > 0 && found == false ) { // used if file name not in Array visited and Array
length >0
sharp(__dirname + “images” + req.query.name + “.jpg”)
.resize(200,200)
.toFile(__dirname + “images_thumb” + req.query.name + “.jpg”)
.then(console.log(“done”)).catch((err)=>{
res.send(‘Not found’);
console.log(err);
});

      res.sendFile(__dirname + "\images\" + req.query.name + ".jpg");
      visited.push(req.query.name);
      console.log(visited);
   

};

 // res.sendFile(__dirname + "\images_thumb\" + req.query.name + ".jpg");
});

app.listen(3000, function () {
   console.log("running");
});

the file structiure as follow
main folder inside is images and images_thumb folder with server.js