欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

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

程序员文章站 2024-01-02 14:00:04
...
这篇文章主要介绍了nodejs实现超简单生成二维码的方法,结合实例形式分析了nodejs基于qr-image插件生成二维码的相关操作技巧,需要的朋友可以参考下

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

一开始使用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>');
  }
})

最后效果

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

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

Vue.js 表单控件操作小结

spirngmvc js传递复杂json参数到controller的实例

JS获取url参数,JS发送json格式的POST请求方法

以上就是nodejs实现超简单生成二维码的方法的详细内容,更多请关注其它相关文章!

上一篇:

下一篇: