1. 程式人生 > >node.js 生成二維碼

node.js 生成二維碼

bsp .get ons wid oca 自己 pre .com aid

  

  因為自己的項目中,想在商品詳情頁上 顯示一個 商品優惠券的二維碼。 以此為需求。

技術分享圖片

node.js 後臺代碼  

const qr_image = require("qr-image");

app.get("/qrcode/txt", async (req, res, next) => {
    const temp_qrcode = qr_image.image(req.query.text);
    res.type("png");
    temp_qrcode.pipe(res);
});

 

前端代碼。 <img src="" /> src中的路徑 是請求後臺生成二維碼的接口路徑

<img
 id="codeimg"
 src="http://localhost/qrcode/txt?text=http://www.baidu.com"
 width="160"
 height="160"
/>

  

node.js 生成二維碼