Skip to content

Instantly share code, notes, and snippets.

@steverhall
Created March 12, 2021 20:55
Show Gist options
  • Save steverhall/9ddb246382ac2b30750c05d535a0ff62 to your computer and use it in GitHub Desktop.
Save steverhall/9ddb246382ac2b30750c05d535a0ff62 to your computer and use it in GitHub Desktop.
Sending a generated PNG in NodeJS/Express
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