Home  >  Article  >  Web Front-end  >  Detailed explanation of the dynamic creation method of QR code based on node.js

Detailed explanation of the dynamic creation method of QR code based on node.js

巴扎黑
巴扎黑Original
2017-08-15 10:10:361862browse

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: &#39;POST&#39;,
   url: &#39;/house&#39;,
   data: {
     data:&#39;你的数据&#39;
   },
   dataType: &#39;json&#39;,
   success: function (data) {
     if (data.code == 0) {
        alert("成功");
        $(&#39;#saoma&#39;).before(&#39;<img class="qrcode_show" src="/newQrCode?url=&#39;+ data.data +&#39;"alt=""/>&#39;);       
     }else {
        alert(&#39;失败&#39;);
     }
   }
})

nodejs:


var express = require(&#39;express&#39;);
var request = require(&#39;request&#39;);
var url = require(&#39;url&#39;);
var qrImg = require(&#39;qr-image&#39;);

//生成二维码
app.get(&#39;/newQrCode&#39;, function (req, res) {
  var par = url.parse(req.url, true).query;
  var thisUrl = par.url;
  var thisParam = par.id;
  var imgUrl = thisUrl + &#39;?id=&#39; + thisParam;
  var img = qrImg.image(imgUrl, { size: 10 });
  res.writeHead(200, { &#39;Content-Type&#39;: &#39;image/png&#39; });
  img.pipe(res);
});


var bodyParser = require(&#39;body-parser&#39;);
var urlencodeRarser = bodyParser.urlencoded({ extended: false });
app.post(&#39;/house&#39;,urlencodeRarser,function (req, res) {
  var data = req.body;
  var result = JSON.stringify({code:0, data:&#39;http://www.baidu.com/&id=&#39; + 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn