我正在尝试在 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 块,但没有成功。我错过了什么?