当前位置: 移动技术网 > IT编程>开发语言>JavaScript > nodejs实现超简单生成二维码的方法

nodejs实现超简单生成二维码的方法

2018年03月30日  | 移动技术网IT编程  | 我要评论

本文实例讲述了nodejs实现超简单生成二维码的方法。分享给大家供大家参考,具体如下:

一开始使用node-qrcode(),结果安装的时候需要安装python,且不支持python3.0以上,安装python2.0的时候又需要安装其他的环境,所以放弃了。

最后选择了一个小众的插件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>');
  }
})

最后效果

ps:这里再为大家推荐两款二维码相关在线工具供大家参考使用:

在线生成二维码工具(加强版)

在线二维码解码识别工具

希望本文所述对大家nodejs程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网