Thursday, November 8, 2012

Node Cookbook, Chapter 6 implementation of sessions, site wide management

Okay reading Node Cookbook.  I am having difficulties with an example here, namely, Chapter 6, custom middle ware for site wide management, under initializing and using a session.  Here the problem encountered namely begins with the alternate use of the deprecated function dynamic helpers().  In this implementation the book calls for


app.locals.use( function (req, res) {
        res.locals.user = req.session.user;
    });
However this gives rise to error at least in my case, so a slight modification of this alternately is


app.use( function (req, res) {
        res.locals.user = req.session.user;
    });
which seems to resolve some issue here, but then I get an error in the views>login.jade file referencing user.

Okay some additional s here of notable mention.

In the views/login.jade  file I had to change the reference from

user

to

locals.user

or notably I received an error that user wasn't defined oddly enough.

Secondly I instantiated the function in the configures portion of the app in the following manner:


app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('kooBkooCedoN'));

  app.use(express.session());
  app.use(require('./login'));
  app.use( function (req, res, next) {
        console.log('hit func1');
        res.locals.user = req.session.user;
        next();
    });
  app.use(app.router);

  app.use(express.static(path.join(__dirname, 'public')));


});

Try moving this app.use() function with res.locals.user assignment just after the app.use(app.router); instantiation and you might as I did have problems with your function being called even though it were technically instantiated, or this is to say, I've found this function has to be called before the app.router instantiation but not before the require('./login') instantiation !!!  Obviously the assigning before the require(./login) assignment references res.locals.user before user has technically been assigned in the ./login module, or at least received an undefined assignment error here, and then it appears that app.router assignments causes disregard in express of any app.use() response, request, next functions needed to set res.local.user...so order of instantiation is very important here.

Since I am a noob when working with express here, it is worth noting that that app.get() function that conflict with app.use() functions on response and requests basis neither coupled may with next(); generate un desired results, or at least I've run into these sorts of problems especially as pertaining to GET page routing.

No comments:

Post a Comment

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...