gpt4 book ai didi

javascript - Node js 如果 url 包含参数则显示 Canvas ,否则显示列表以重定向到 Canvas

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:22 24 4
gpt4 key购买 nike

我正在尝试检查是否加载了包含 url post 参数的 node.js 网站,如果 url 中没有包含 post 参数,那么它应该显示一个表单并使用 url 参数将用户引导到同一网站。

但我不知道如何检查网址是否包含以下参数:?room=xxx

如果确实包含它,则显示 Canvas 。如果没有,则加载列表并在单击列表项时将用户重定向到包含 url 参数的同一站点。

我当前的代码:

world.handlebars:

{{#if room}}
<canvas id="canvas"></canvas>
<div id="textChat"></div>

<script>
(function() {
var canvas = document.getElementById('canvas'), context = canvas.getContext('2d');
window.addEventListener('resize', resizeCanvas, false);

function resizeCanvas()
{
var width = window.innerWidth;
var height = window.innerHeight - 100;
canvas.width = width;
canvas.height = height;
drawStuff(width, height);
}

resizeCanvas();

function drawStuff(width, height)
{
context.fillStyle = "#68BBE3";
context.fillRect(0, 0, width, height);

// Element
grd = context.createLinearGradient(150.000, 0.000, 150.000, 300.000);
grd.addColorStop(0.000, 'rgba(156, 218, 231, 1.000)');
grd.addColorStop(0.468, 'rgba(113, 192, 225, 1.000)');
grd.addColorStop(1.000, 'rgba(88, 178, 205, 1.000)');
context.fillStyle = grd;
context.fillRect(0, 0, width, height / 100 * 40);
// End Element

// Element
grd = context.createLinearGradient(83.333, 0.000, 216.667, 300.000);
grd.addColorStop(0.000, 'rgba(120, 175, 47, 1.000)');
grd.addColorStop(0.464, 'rgba(145, 191, 92, 1.000)');
grd.addColorStop(0.996, 'rgba(154, 195, 101, 1.000)');
context.fillStyle = grd;
context.fillRect(0, height / 100 * 40, width, height / 100 * 60);
}

})();
</script>
{{else}}
<div class="container">
<h4 class="page-header">Dashboard</h4>
<div class="list-group">
<a href="?room=test" class="list-group-item">First item</a>
<a href="#" class="list-group-item">Second item</a>
<a href="#" class="list-group-item">Third item</a>
</div>
</div>
{{/if}}

world.js:

var express = require('express');
var router = express.Router();

// Get Site
router.get('/', ensureAuthenticated, function(req, res){
res.render('world', {username: req.user.username});
});

function ensureAuthenticated(req, res, next){
if(req.isAuthenticated())
{
return next();
}
else
{
req.flash('error_msg', 'You are not logged in');
res.redirect('/users/login');
}
}

module.exports = router;

最佳答案

您可以按照 Express documentation 访问 request.query 中的变量.

var room = req.query.room;

在这里,我尝试复制您的要求。

var express = require('express');
var router = express.Router();

// Get Site
router.get('/', ensureAuthenticated, function(req, res){
if (!req.query.room) {
// Room is not in URL. Handle redirect here.
}
next()
}, function (req, res) {
res.render('world', {username: req.user.username});
})

function ensureAuthenticated(req, res, next){
if(req.isAuthenticated())
{
return next();
}
else
{
req.flash('error_msg', 'You are not logged in');
res.redirect('/users/login');
}
}

module.exports = router;

关于javascript - Node js 如果 url 包含参数则显示 Canvas ,否则显示列表以重定向到 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44153366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com