我正在尝试生成一条路由,该路由可用于根据传入的orderId生成条形码,但是jsbarcode未呈现。
这是我的路线代码
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;
最终目标是能够使用此路线生成带有条形码的图像,其中条形码带有从路线传递的订单号。
答案 0 :(得分:0)
首先,您需要获取orderId。为此,您必须使用req.params.orderId
。
第二,当您致电res.send
时,它将结束您的通话,因此您无法处理要发送的任何内容作为响应。
第三,我不知道您到底想如何使用canvas和jsBarcode库,而是将orderId放在需要的地方。
正确的编码方式可能是:
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);
});