node.js 生成二维码

  

  因为自己的项目中,想在商品详情页上 显示一个 商品优惠券的二维码。 以此为需求。

 

 

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"
/>

  

 

posted @ 2018-09-04 22:34  Mr丶Lee  阅读(2960)  评论(0编辑  收藏  举报