Home > Article > Web Front-end > Nodejs implements a super simple method to generate QR codes
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='stylesheet' href='/stylesheets/style.css'/> </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('qr-image') router.get('/', function (req, res, next) { res.render('index', {title: 'Express'}); }); router.get('/create_qrcode', function (req, res, next) { var text = req.query.text; try { var img = qr.image(text,{size :10}); res.writeHead(200, {'Content-Type': 'image/png'}); img.pipe(res); } catch (e) { res.writeHead(414, {'Content-Type': 'text/html'}); res.end('<h1>414 Request-URI Too Large</h1>'); } })
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!