发布时间:2019-06-30编辑:佚名阅读(1564)
安装模块
npm install 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=https://www.suajin.com/"/> </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>'); } })
最后效果
0人
0人
0人
0人