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 and add the server configuration and routing logic:
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 running at http://localhost: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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Node.js Barcode Generator</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" crossorigin="anonymous">
</head>
<body>
<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 Barcode</button>
</form>
</td>
</tr>
<tr>
<td>
<% if (data !== '') { %>
<img src="<%= data.replace(/"/g, "") %>" class="form-control" alt="Generated Barcode">
<% } %>
</td>
</tr>
</table>
</div>
</body>
</html>
Step 4: Run and Test Your Application
- Start your server in your terminal:
node index.js - Open your web browser and navigate to:
http://localhost:3000 - Type any text into the input field and click Generate Barcode to view your server-rendered barcode instantly!
References & Source Code
Video Tutorial: Watch Video Tutorial on YouTube
Source Code: Download Source Code on GitHub
Comments
Post a Comment