首頁  >  文章  >  web前端  >  nodejs產生二維碼(最簡潔)

nodejs產生二維碼(最簡潔)

php中世界最好的语言
php中世界最好的语言原創
2018-05-10 14:17:443676瀏覽

這次帶給大家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>

後端程式碼:

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中文網其它相關文章!

推薦閱讀:

aggregate級聯查詢實現步驟

#JS把連結產生二維碼圖片方法分析

以上是nodejs產生二維碼(最簡潔)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn