一开始使用node-qrcode(https://github.com/soldair/node-qrcode),结果安装的时候需要安装python,且不支持python3.0以上,安装python2.0的时候又需要安装其他的环境,所以放弃了。
最后选择了一个小众的插件qr-image(https://github.com/alexeyten/qr-image)
前台页面如下
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>
后端代码:
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>'); } })
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是nodejs生成二维码(最简洁)的详细内容,更多请关注php中文网其它相关文章!
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论