How to Generate Barcodes in Node.js Using Express, EJS, and JsBarcode

Generating barcodes programmatically is a frequent requirement when building inventory systems, invoicing modules, or shipping portals. While many developers rely on heavy client-side libraries or external APIs, you can easily build a server-side barcode generator using Node.js, Express, EJS, and the Canvas package.

In this tutorial, we will walk through a complete implementation of a dynamic barcode web app that accepts user input via a form, generates a barcode image server-side using JsBarcode and canvas, and renders it instantly on an EJS template.

Prerequisites & Tech Stack

Make sure you have Node.js installed on your system. We will use the following packages:

  • Express: Fast, unopinionated web framework for Node.js.

  • EJS: Embedded JavaScript templating engine.

  • Canvas: To provide a headless HTML5 Canvas implementation in Node.js.

  • JsBarcode: A lightweight barcode generation library.


Step 1: Initialize Project and Install Dependencies

Open your terminal, create a new directory for your project, initialize the package, and install the required dependencies:

mkdir barcode

cd barcode

npm init -y

npm install express ejs canvas jsbarcode


Step 2: Set Up the Express Server (index.js)

Create your main server file named index.js.

const express   = require('express');

const { Canvas } = require("canvas");

const JsBarcode = require('jsbarcode');

const app = express();

app.set('view engine', 'ejs');

app.use(express.urlencoded({extended:false}));

app.use(express.json());


app.get('/',(req,res)=>{

     res.render('index',{data:''});

})


app.post('/',(req,res)=>{

  const canvas = new Canvas();

 JsBarcode(canvas,req.body.qrcodee,{

  lineColor: "#0aa",

  width:4,

  height:40,

  displayValue: false

  });

     canvas.toDataURL('image/png', (err, png) => {

    console.log(png);

    res.render('index',{data:png});

  })

})

app.listen(3000,console.log(`server run at 3000`));


Step 3: Create the EJS Template (views/index.ejs)

Create a folder named views, and inside it, create a file named index.ejs. This template includes a Bootstrap-styled form for user input and dynamically renders the generated barcode image.

<html>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css"  crossorigin="anonymous">

    <div class="container" style="margin-top: 100px;">

           <table border="1" class="table"> 

             <tr>

                <td>

                <form action="/" method="post">

                    <input type="text" name="qrcodee" placeholder="enter barcode text" class="form-control"><br>

                    <button  class="btn btn-primary" type="submit">Generate Qrcode</button>

                </form>    

            </td>

             </tr>

             <tr>

               <td>

                <%if(data!=''){%>

                    <img src="<%= data.replace(/"/g,"") %>" class="form-control">

                    <%}%>

               </td>

             </tr>

           </table>

    </div>

</html>


Step 4: Run and Test Your Application

  1. Start your server in your terminal:    node index.js

  2. Open your web browser and navigate to:

    • http://localhost:3000

  3. Type any text  into the input field and click Generate Barcode to view your server-rendered barcode instantly!




Comments

Popular posts from this blog

How to Setup a Flask Project in Python: Step-by-Step Guide

How to Use Sequelize-CLI to Create Tables and Relationships in Node.js

Hashing Password With Bcrypt In Node