I am trying to generate a QR-code on an express request. It takes the value from the URL parameter, and returns the QR-Code using a filestream as a raw image.
const express = require('express');
const router = express.Router();
const QRCode = require('qrcode');
router.get('/qr/:content', function(req, res, next){
let content = req.params.content
// Filestream goes here
})
This is how I tried to do it, however I have never worked with filestreams and I cannot get it working:
let code = QRCode.toFileStream(new stream.Writable, conent)
code.pipe(res);
This is the library I'm using: https://www.npmjs.com/package/qrcode
Try this code:
import QRCode from 'qrcode';
import { PassThrough } from 'stream';
router.get('/qr/:content', async (req, res, next) => {
try{
const content = req.params.content;
const qrStream = new PassThrough();
const result = await QRCode.toFileStream(qrStream, content,
{
type: 'png',
width: 200,
errorCorrectionLevel: 'H'
}
);
qrStream.pipe(res);
} catch(err){
console.error('Failed to return content', err);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With