Getting object from url to render a barcode using jsbarcode

622 Views Asked by At

I am trying to generate a route that I can use to generate a barcode based on the orderId that its passed in, however, the jsbarcode is not rendering.

This is my route code

let express = require('express');
let router = express.Router();
let url = require('url');
let JsBarcode = require('jsbarcode');
let Canvas = require('canvas')


router.get('/:orderId', function(req, res, next) {
res.send( req.param('orderId'));

var canvas = new Canvas();
JsBarcode(canvas, "Hello"); //I want to eventually pass the orderID here so that it can generate the barcode based on that.  


});

module.exports = router;

The end goal is to be able to use this route to generate an image with a bardcode with the order number passed in from the route.

1

There are 1 best solutions below

2
On

First, you need to get your orderId. To do that, you have to use req.params.orderId.

Second, when you call res.send, it ends your call, so you can't process anything you want to send in response.

Third, I don't know exactly how you want to use the canvas and jsBarcode libraries but put the orderId where you need it.

The right way to code it could be:

router.get('/:orderId', function(req, res, next) {
  const orderId = req.params.orderId; 

  var canvas = new Canvas();
  const barcode = JsBarcode(canvas, "Hello"); //I want to eventually pass the orderID here so that it can generate the barcode based on that.  

  res.send(barcode);
});