Created
March 12, 2021 20:55
-
-
Save steverhall/9ddb246382ac2b30750c05d535a0ff62 to your computer and use it in GitHub Desktop.
Sending a generated PNG in NodeJS/Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import healthCardQR from 'express'; | |
import * as shc from '../smartHealthCard'; | |
export const create = async (req: healthCardQR.Request, res: healthCardQR.Response) => { | |
try { | |
var qrCodeImage = await shc.queryGetQRCode(req.params.id); | |
// remove data:image prefix in image object | |
const img = Buffer.from(qrCodeImage.split(",")[1], 'base64'); | |
res.contentType('image/png'); | |
res.setHeader('Content-Length', img.length); | |
res.end(img); // Send the file data to the browser. | |
} catch (err) { | |
res.statusCode = 500; | |
res.send("Error rendering QR code for this id."); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment