Home > Node.js > How to create or generate QR code using Node.js

How to create or generate QR code using Node.js

In this post I would like to explain about how to create or generate the QR code using Node.js with dynamic data. It is simple code to generate the code, but it will help in many of our applications.

  • For the accessing any content or links rather than display directly, display it in QR codes. It is a secure way to display those contents.
  • What we are going to achieve with QR code, simple display it in your website for the apps links, websites, amount related.
  • How to create or generate QR code using Node.js by Anil Labs

    How to create or generate QR code using Node.js by Anil Labs

  • How we are going to create or generate it
    1. As earlier explained about install the Nodejs in your system.
    2. How to install the packages
    To install any packages we have to follow below command to install it globally

    npm install -g [package name]

    To install any packages we have to follow below command to install it application

    npm install –save [package name]

    3. In our application we need to install the below packages

    npm install --save body-parser</code>
    <code>npm install --save express</code>
    <code>npm install --save fs</code>
    <code>npm install --save path</code>
    <code>npm install --save qrcode

    4. Write the PORT number inside of your index.js or app.js file

    const PORT = 3000

    5. Include the all packages in your app file

    const express = require('express');
    var bodyParser = require('body-parser');
    var path = require('path');
    var fs = require("fs");
    var QRCode = require('qrcode');

    6. Include the body parser and express in your code

    var app = express();
    app.set('view engine', 'html');
    app.use(bodyParser.json({limit: '50mb'}));
    // parse requests of content-type - application/json
    //app.use(bodyParser.urlencoded({ extended: true }))
    app.use(bodyParser.urlencoded({ extended: true ,keepExtensions: true}));
    // parse requests of content-type - application/json
    app.use(bodyParser.json())

    7. CORS for our application

    app.use(function(req, res, next) { //allow cross origin requests
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
    });

    8. Middileware for our application

    const logger = function (req, res, next) {
      //console.log(req)
      next()
    }

    app.use(logger)

    9. Generate the QR code with provide text

    app.get('/', (req, res) => {
        QRCode.toDataURL('https://anillabs.com', function (err, url) {
            templatePath = path.join(__dirname + '/code.html');
            fs.readFile(templatePath, {encoding: 'utf-8'}, function(err,data){
                data = data.replace(/##qUrl/g, url);
                res.send(data)
            })
          })
    })

    10. Application listen

    app.listen(PORT, () => console.log('Anil Labs QR Code generation on the port: ' + PORT))

    11. To display the code in your HTML file

    <img src="##qUrl" />

    12. Run the application

    node index.js

How to create or generate QR code using Node.js by Anil Labs

How to create or generate QR code using Node.js by Anil Labs


with the above the steps we can create or generate the QR code using Node.js

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.