redirect if no cookie in express.js and socket.io

how should i redirect or write something for unauthorized user in my application

i want to redirect page if no cookie defined or cookie value is null

here is my script and i specified where i think, i should to add something.

    var express = require('express'),
        app = express(),
        memcache = require("memcache"),
        http = require('http'),
        server = http.createServer(app),
        io = require('socket.io').listen(server),
        co = require("./cookie.js"),
        codein = require("node-codein");

    //check if user loggedin
    // routing
    app.get('/', function (req, res) {
      res.sendfile(__dirname + '/index.html');
    });





    io.configure(function (){
              io.set('authorization', function (data, accept) {
                    var cookieManager = new co.cookie(data.headers.cookie);

                    var client = new memcache.Client(11211, "localhost");
                    client.connect();

                    client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
                        if(result){
                            var session = JSON.parse(result);
                            user = JSON.parse(session.name);
                            user = user.username;
                        }

                  if (typeof result === 'undefined' || (!result)) {
//------------> here if no cookie redirect to noaccess.html or just write you have no access to this page
                    return accept('No cookie transmitted.', false);

                  } else {
//------------> here if there is cookie then let users to see index.html
                    data.sessionID = cookieManager.get("sec_session_id");
                    accept('cookie recieved', true);
                  } 


                io.on('connection', function(socket) {
                //console.log(socket.handshake.sessionID);
                });

            });
                });
        });




    server.listen(3000);

i tryed everything but no luck in this

thanks in advance...

Considering that you want to redirect the user, it would make more sense to check for the cookie within an app.get callback, because you now have access to the response object.

If you want to intercept every request, simply put a block like this in your code:

app.get("/*", function(req, res, next){

    if(req.cookies){ // or req.cookies['cookiename'] if you want a specific one
        res.render("index.html");
    }   

    else{
        res.render("noaccess.html");
    }
    //You can optionally put next() here, depending on your code
});

in my case i think i need to emit if access granted then we have this in server side

io.on('connection', function(socket) {
            //console.log(socket.handshake.sessionID);
            socket.emit('access', 'access granted');
            });
    });

and in client we have this form

<html>
  <head>
    <title> Tying it all together demo</title>
    <script src='/socket.io/socket.io.js'></script>
    <script src='http://code.jquery.com/jquery-latest.js'></script>
    </head>
  <body>
    <p id="text">access denied</p>
    <script>
        var socket = io.connect('http://localhost:3000/');
        socket.on('access', function (data) {
            $("#text").html(data);
        });

    </script>
</body>

then if we have access we emit access function in server and access denied text will change to access granted