Home > Web Front-end > JS Tutorial > Nodejs implements a super simple method to generate QR codes

Nodejs implements a super simple method to generate QR codes

亚连
Release: 2018-05-29 16:37:40
Original
2599 people have browsed it

This article mainly introduces the method of nodejs to achieve super simple generation of QR codes, and analyzes the related operation skills of nodejs based on the qr-image plug-in to generate QR codes in the form of examples. Friends in need can refer to it

The example in this article describes how nodejs implements a super simple method of generating QR codes. Share it with everyone for your reference, the details are as follows:

Use node-qrcode (https://github.com/soldair/node-qrcode) at the beginning, but you need to install it during the installation. python, and does not support python3.0 or above. When installing python2.0, you need to install 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>
Copy after login

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;);
  }
})
Copy after login

Finally Effect

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Vue.js form control operation summary

Instance of spirngmvc js passing complex json parameters to controller

JS gets url parameters, JS sends POST request method in json format

The above is the detailed content of Nodejs implements a super simple method to generate QR codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template