nodejs實現(xiàn)超簡單生成二維碼的方法
本文實例講述了nodejs實現(xiàn)超簡單生成二維碼的方法。分享給大家供大家參考,具體如下:
一開始使用node-qrcode(https://github.com/soldair/node-qrcode),結(jié)果安裝的時候需要安裝python,且不支持python3.0以上,安裝python2.0的時候又需要安裝其他的環(huán)境,所以放棄了。
最后選擇了一個小眾的插件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>');
}
})
最后效果

PS:這里再為大家推薦兩款二維碼相關(guān)在線工具供大家參考使用:
在線生成二維碼工具(加強版)
http://tools.jb51.net/transcoding/jb51qrcode
在線二維碼解碼識別工具
http://tools.jb51.net/transcoding/trans_qrcode
希望本文所述對大家nodejs程序設(shè)計有所幫助。
相關(guān)文章
NestJS中集成TypeORM進行數(shù)據(jù)庫操作
本文深入探討了如何在NestJS中集成TypeORM進行數(shù)據(jù)庫操作,包括TypeORM的配置和集成、實體設(shè)計和關(guān)系映射、Repository模式的應(yīng)用、事務(wù)處理方案、數(shù)據(jù)庫遷移管理、性能優(yōu)化策略2024-12-12
詳解nodejs express下使用redis管理session
本篇文章主要介紹了詳解nodejs express下使用redis管理session ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
基于Koa(nodejs框架)對json文件進行增刪改查的示例代碼
這篇文章主要介紹了基于Koa(nodejs框架)對json文件進行增刪改查的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

