Home  >  Article  >  Web Front-end  >  Nodejs super simple method to generate QR code

Nodejs super simple method to generate QR code

小云云
小云云Original
2018-03-19 09:12:352822browse

This article mainly introduces nodejs to achieve a super simple method of generating QR codes, and analyzes the related operating skills of nodejs based on the qr-image plug-in to generate QR codes in the form of examples. Friends who need it can refer to it and use it at the beginning. node-qrcode(https://github.com/soldair/node-qrcode),As a result, you need to install python when installing, and it does not support python3.0 or above. You need to install it when installing python2.0 Other environments, so I gave up.

Finally chose a niche plug-inqr-image(https://github.com/alexeyten/qr-image)

The front page is as follows

views/index.ejs


<!DOCTYPE html>
<html>
<head>
  <title><%= title %></title>
  <link rel=&#39;stylesheet&#39; href=&#39;/stylesheets/style.css&#39;/>
</head>
<body>
<h1><%= title %></h1>
<img src="/create_qrcode?text=http://blog.csdn.net/fo11ower"/>
</body>
</html>

Backend code:

routes/index. js


var qr = require(&#39;qr-image&#39;)
router.get(&#39;/&#39;, function (req, res, next) {
  res.render(&#39;index&#39;, {title: &#39;Express&#39;});
});
router.get(&#39;/create_qrcode&#39;, function (req, res, next) {
  var text = req.query.text;
  try {
    var img = qr.image(text,{size :10});
    res.writeHead(200, {&#39;Content-Type&#39;: &#39;image/png&#39;});
    img.pipe(res);
  } catch (e) {
    res.writeHead(414, {&#39;Content-Type&#39;: &#39;text/html&#39;});
    res.end(&#39;<h1>414 Request-URI Too Large</h1>&#39;);
  }
})

Related recommendations:

JS method to generate a QR code from a link and convert it into an image

JS generates QR code

phpqrcode class generates QR code method

The above is the detailed content of Nodejs super simple method to generate QR code. 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