In this article, we will explain to you how to generate QR code in node js application. so we will give you a simple example of a node QR code generator. They are usually used to encode a URL so that someone can simply scan the code and visit a site.
A QR code (Quick Response Code) is a machine-readable optical identifier that can include information about the item to which the code is attached.
so you can see below the node js qr code example.
First, we will open the command prompt and create the application in our directory. for this, you can follow the below command.
mkdir my_node_app cd my_node_app npm init
After the done setup node js application, we will install the express and qrcode npm package. so you can see the following npm command.
npm install express --save npm install qrcode --save
server.js
var express=require('express');
var app = express();
var QRCode = require('qrcode');
app.get('/',function(req,res){
// Creating the data
let data = {
name:"test",
age:27,
email:"[email protected]"
}
// Converting the data into String format
let stringdata = JSON.stringify(data);
// Print the QR code to terminal
QRCode.toString(stringdata,{type:'terminal'},function (err, QRcode) {
if(err){
return console.log("error occurred");
}else{
// Printing the generated code
console.log(QRcode);
}
});
// Converting the data into base64
QRCode.toDataURL(stringdata, function (err, code) {
if(err){
return console.log("error occurred");
}else{
// Printing the code
console.log(code);
}
})
});
app.listen(3000,function(){
console.log("Express Started on Port 3000");
});
we will run the node js application using the below command. so you can follow the below command.
node server.js
Now you can run the example using the below Url in the browser.
http://localhost:3000