简体   繁体   English

快速路线中的捕获错误不起作用

[英]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")我正在 node.js 创建 API 它是 function 如下 这个 API 将用于从 Images 文件夹发送图像然后存储图像名称,如果服务器收到任何图像请求,则在使用 Sharp 调整图像大小后已发送到 images_thumb 文件夹在 API 之前发送的应该从 Images_thumb 文件夹发送图像 ----Proplem 每当我打开浏览器到 http://localhost:3000/?name=6(6 是图像文件夹中不存在的图像名称例如)图像名称被添加到访问数组,我不知道如何发送 res.send(“找不到文件”)

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文件结构如下,里面的主文件夹是 images 和带有 server.js 的 images_thumb 文件夹

Try to console.log this: __dirname + "\\images\\" + req.query.name + ".jpg" , and see the path, probably is not correct.尝试 console.log 这个: __dirname + "\\images\\" + req.query.name + ".jpg" ,看看路径,可能是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM