我需要获取完整的url作为参数,以将其转换为qrcode。网址的格式为https://website.com.br/2k后,我将无法在node.js路由中将其作为参数接收
这就是我将参数发送到ejs页面的方式
res.render("tutorial.ejs", { qrcode: '/qrcode/'+ link });
link
类似于:http://comp.co/32
在tutorial.ejs中,我渲染了调用qrcode路由的qrcode
<img src="<%= qrcode %>">
二维码路由:
routes.get('/qrcode/:url',(req,res, next) => {
const code = qr.image(req.params.url, {type: 'svg'});
res.type('svg');
code.pipe(res);
})
不起作用。我认为发生这种情况是因为我的qrcode路由正在获取类似以下内容的参数:http://comp.co/32
答案 0 :(得分:1)
when you make get request, you are basically calling
routes.get(/qrcode/http:/comp.co/32){...}
so router in the backend understands it as a route and it wont call your expected route i.e.
routes.get(/qrcode/:url){...}
Solution:
Url encode your url as and try your code again.
var link="http%3A%2F%2Fcomp.co%2F32"
res.render("tutorial.ejs", { qrcode: '/qrcode/'+ link });
<img src="<%= qrcode %>">
routes.get('/qrcode/:url',(req,res, next) => {
const code = qr.image(req.params.url, {type: 'svg'});
res.type('svg');
code.pipe(res);
})
But better way to pass parameter is using query to avoid confusion, you can do this
res.render("tutorial.ejs", { qrcode: '/qrcode?url='+ link });
routes.get('/qrcode',(req,res, next) => {
const code = qr.image(req.query.url, {type: 'svg'});
res.type('svg');
code.pipe(res);
})
Hope it solves your problem.
答案 1 :(得分:0)
您可以在qrcode路由内的console.log(req)来查看可以从请求中获取什么数据。