1

我正在尝试在 Typescript 中使用 express 获取图像 URL,对其进行过滤并将图像作为响应发送回。这是端点:

app.get("/filteredimage/", async (req, res) =>{
try{
  let {image_url} = req.query;
  if(!image_url){
    return res.status(400).send("bad request!");
  }
  console.log(image_url);
  const path = await filterImageFromURL(image_url);
  res.sendFile(path);
  res.on('finish', () => deleteLocalFiles([path]));
} catch {
  return res.status(500).send({error: 'Unable to process your request'});
}
});

过滤图片url函数如下:

export async function filterImageFromURL(inputURL: string): Promise<string>{
return new Promise( async resolve => {
    const photo = await Jimp.read(inputURL);
    const outpath = '/tmp/filtered.'+Math.floor(Math.random() * 2000)+'.jpg';
    await photo
    .resize(256, 256) // resize
    .quality(60) // set JPEG quality
    .greyscale() // set greyscale
    .write(__dirname+outpath, (img)=>{
        resolve(__dirname+outpath);
    });
});
}

但是在到达端点时,我收到以下错误:

(node:23042) UnhandledPromiseRejectionWarning: Error: Could not find MIME for Buffer <null>
at Jimp.parseBitmap (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/utils/image-bitmap.js:73:15)
at Jimp.call [as parseBitmap] (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/index.js:395:17)
at parseBitmap (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/index.js:339:14)
at cb (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/index.js:68:14)
at cb (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/request.js:47:9)
at IncomingMessage.<anonymous> (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/phin/lib/phin.compiled.js:1:2038)
at IncomingMessage.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:23042) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error 
originated either by throwing inside of an async function without a catch block, or by 
rejecting a promise which was not handled with .catch(). To terminate the node process on 
unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see 
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:23042) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In 
the future, promise rejections that are not handled will terminate the Node.js process with 
a non-zero exit code.

尝试在两个函数中添加 try-catch 块,但没有成功。我错过了什么?

4

2 回答 2

1

我正在做同样的课程,问题是示例图片(https://timedotcom.files.wordpress.com/2019/03/kitten-report.jpg)不再存在。一个简洁的错误处理需要修改提供的 util 代码:

export async function filterImageFromURL(inputURL: string): Promise<string> {
    return new Promise((resolve, reject) => {
        Jimp.read(inputURL).then(photo => {
            const outpath = '/tmp/filtered.' + Math.floor(Math.random() * 2000) + '.jpg';
            photo
                .resize(256, 256) // resize
                .quality(60) // set JPEG quality
                .greyscale() // set greyscale
                .write(__dirname + outpath, (img) => {
                    resolve(__dirname + outpath);
                });
        }).catch(err => {
            console.error(err);
            reject("Could not read image.");
        })
    });
}

通过此修改,您可以对代码片段中的此错误做出反应。

于 2020-09-18T11:29:55.667 回答
0

进行 Jimp.read() 调用时,似乎没有捕获到错误。

请尝试以下代码段。

    return new Promise(async (resolve, reject) => {
        try {
            const photo = await Jimp.read(inputURL);
            const outpath = '/tmp/filtered.' + Math.floor(Math.random() * 2000) + '.jpg';
            await photo
                .resize(256, 256) // resize
                .quality(60) // set JPEG quality
                .greyscale() // set greyscale
                .write(__dirname + outpath, (img) => {
                    resolve(__dirname + outpath);
                });

        } catch (error) {
            reject(error)
        }
    });
}
于 2020-09-18T12:13:27.680 回答