In this article, we will see how to generate QR Code in the Node.js application. we will use qrcode npm package to generate QR Code. we will create a JSON data object and create a QR Code for that JSON data object.
QR code has become an important part of life now a days. QR means “Quick Response”. It can store a large amount of data. QR scanners can instantly process the data scanning it. In Node.js generating QR Code is very easy.
So, let's see generate QR code in node js and QR code generator node js.
In this step, create a node application using the below commands.
mkdir qr_code_example
cd qr_code_example
npm init
Install the qrcode module using the below command
npm install qrcode
Now, we can import the qrcode package into the index file and generate a QR code.
const qr = require('qrcode');
let data = {
id: 1,
name: "dell",
email: "dell@yopmail.com"
};
let strData = JSON.stringify(data);
qr.toString(strData, {type:'terminal'},
function (err, code) {
if(err) return console.log("error occurred !!");
console.log(code);
});
qr.toDataURL(strData, function (err, code) {
if(err) return console.log("error occurred !!");
console.log(code);
})
const data = {
errorCorrectionLevel: 'H',
type: 'terminal',
quality: 0.95,
margin: 1,
color: {
dark: '#208698',
light: '#FFF',
},
}
let strData = JSON.stringify(data)
For Custom QR Code generation you need to define different parameters like the above code.
Now, run the index.js using the below code.
node index.js
You might also like: