Home > Web Front-end > JS Tutorial > body text

Nodejs generates QR code (the simplest)

php中世界最好的语言
Release: 2018-05-10 14:17:44
Original
3707 people have browsed it

这次给大家带来nodejs生成二维码(最简洁),nodejs生成二维码的注意事项有哪些,下面就是实战案例,一起来看一下。

一开始使用node-qrcodehttps://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=&#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

后端代码:

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

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

aggregate级联查询实现步骤

JS把链接生成二维码图片方法分析

The above is the detailed content of Nodejs generates QR code (the simplest). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!