This article mainly introduces in detail the method of dynamically creating QR codes in nodejs. It has certain reference value. Interested friends can refer to it.
The examples in this article share nodejs dynamics with everyone. The specific code to create the QR code is for your reference. The specific content is as follows
<!--弹出二维码--> <p class="qrcode"> <p> <p id="saoma">扫描二维码</p> </p> </p>
js code:
$.ajax({ type: 'POST', url: '/house', data: { data:'你的数据' }, dataType: 'json', success: function (data) { if (data.code == 0) { alert("成功"); $('#saoma').before('<img class="qrcode_show" src="/newQrCode?url='+ data.data +'"alt=""/>'); }else { alert('失败'); } } })
nodejs:
var express = require('express'); var request = require('request'); var url = require('url'); var qrImg = require('qr-image'); //生成二维码 app.get('/newQrCode', function (req, res) { var par = url.parse(req.url, true).query; var thisUrl = par.url; var thisParam = par.id; var imgUrl = thisUrl + '?id=' + thisParam; var img = qrImg.image(imgUrl, { size: 10 }); res.writeHead(200, { 'Content-Type': 'image/png' }); img.pipe(res); });
var bodyParser = require('body-parser'); var urlencodeRarser = bodyParser.urlencoded({ extended: false }); app.post('/house',urlencodeRarser,function (req, res) { var data = req.body; var result = JSON.stringify({code:0, data:'http://www.baidu.com/&id=' + data.data}); //console.log(result) res.end(result); });
The above is the detailed content of Detailed explanation of the dynamic creation method of QR code based on node.js. For more information, please follow other related articles on the PHP Chinese website!