简体   繁体   中英

Getting object from url to render a barcode using jsbarcode

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.

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM