Wednesday 24 May 2017

Node js if url contains parameter show canvas otherwise show list to redirtect to canvas

I'm trying to check if a node.js site was loaded containing a url post parameter, if there's no post parameter contained in the url then it should show a form and direct the user to the same site with the url parameter.

But I don't get how to check the url if it contains the following parameter: ?room=xxx

if it does contain it, then show the canvas. If not then load the list and redirect the user on click on a list item to the same site containing the url parameter.

My current code:

world.handlebars:


<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>

<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>


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;



via d4ne

No comments:

Post a Comment