node.js - Nodejs image from readStream gets displayed but connection is still open for a minute or so -
i'm trying create profile picture upload feature , i'm stuck on retrieval part. image gets downloaded in chunks, , once gets rendered reason there's still spinning icon next image in network tab 60seconds, it's image downloaded , rendered still takes minute or bit longer finish loading in network tab of browser, , if try upload image in mean time doesn't rendered until minute or of uploaded image passes, me figure out please.
const storage = require('@google-cloud/storage'); const gcs = storage({ projectid: 'my_project_id', keyfilename: 'keys.json' }); const bucketname = 'my-profile-photos'; const bucket = gcs.bucket(bucketname); const retrievefromgcs = (req, res, next) => { const stream = bucket.file(req.params.name).createreadstream(); res.writehead(200, {'content-type': 'image/jpg' }); stream.on('data', (data) => { console.log('hit stream on data'); res.write(data); }); stream.on('error', (err) => { console.log('hit error'); next(err); }); stream.on('finish', () => { console.log('hit finish'); stream.end(); });
//in case console.logs useful anyone, 'hit stream on data' gets printed around 200 times , 'hit finish' gets printed once
here when streaming finish have end res.
for use below code
stream.on('finish', () => { console.log('hit finish'); stream.end(); res.end(); });
to complete network loader. must have end our response object. browser know there no data write in response now.
Comments
Post a Comment