I am trying to fetch images from google cloud storage
bucket from browser and serving the files using API in express. Following code breaks when the Image path is invalid. The try catch doesn't catch the file not found error. Am I missing something here? This code works if an image file exists.
From the Express side, I am using wildcard route (i.e. /*) as the API can accept any image path coming in and try to serve it.
const express = require('express')
const {Storage} = require('@google-cloud/storage');
let server = express()
const storage = new Storage();
const bucketName = '<some-bucket-name>'
server.get('/*', async (req, res) => {
const widthString = req.query.width
const heightString = req.query.height
const format = req.query.format
const fileName = req.path.substring(1);
console.log("url: ", req.path)
let width, height
if (widthString) {
width = parseInt(widthString)
}
if (heightString) {
height = parseInt(heightString)
}
res.type(`image/${format || 'png'}`)
try{
await storage.bucket(bucketName).file(fileName).createReadStream().pipe(res)
} catch (err) {
console.error(err);
}
})
Error:
url: /media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
events.js:377
throw er; // Unhandled 'error' event
^
ApiError: No such object: assets/media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
at new ApiError (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:73:15)
at Util.parseHttpRespMessage (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:175:41)
at Util.handleResp (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:149:76)
at Duplexify.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/storage/build/src/file.js:888:31)
at Duplexify.emit (events.js:400:28)
at PassThrough.emit (events.js:400:28)
at onResponse (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:222:19)
at PassThrough.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:163:11)
at PassThrough.emit (events.js:412:35)
at /home/ubuntu/imageoptimizer/node_modules/teeny-request/build/src/index.js:191:27
Emitted 'error' event on PassThrough instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
code: 404,