Monday, November 26, 2012

Node Cookbook, Chapter 6 Making an express web application

Now the particular issue that I've run into with this example is implementation of Jade mixin s in this example.  One I would indicate that the mixin in Jade is simply a short hand java script function for the Jade scripting system (inspired incidentally by Haml for Node).  Unfortunately, the Jade script, Profiles.Jade file includes what should apparently be defined as two empty functions which it appears automatically generate an error, but handled by the index.js through the main body apps routes/index.js script...sort of circuitous silliness in my opinion :) ...umm...book example script appears in some ways to be a mess, but... 

Monday, November 19, 2012

Node JS and Express 3 example problem, Child express application called inside a Parent express application

 The problem that I've seen though Express with Node JS, is stable implementation examples keeping to framework versions.  For example, a Node cookbook seems to provide excellent examples say from Express 2, but unfortunately at the time of writing either owing to changes since then through version or for any other reasons, I've spent time studying and trying to re work examples for basic functionality.  I'd give the latest problem example:  This would include using a app.use() call on another express app embedded into a main express app.  Apparently while mount uri citation works, the uri is structurally not integrated into the main express apps router for some odd reason.  I'd indicate that I've been able to use app.use() with uri for mounting functions with resend and request calls implementing by default routing...by the way if you use a console.log(app.routes) call to see the uri as route, it won't show, but will if you alternately use app.get() for the same function.
     Why the big fuss over the call of one app to another?  Well according to the Node cookbook chapter 6 example, going from sub chapter to the next, implementation of previous application functionality can be done so (in a class/module oriented) way simply copying the contents of one application and embedding it in the parent application.  Apparently the very basics of this functionality is handled with a

app.use(uri, require('./myappname/app'));

No problem here at least initially.  The problem that seems to manifest, at least in the latest version of Express somewhere in the process of child apps sending and receiving request or sending response.  At least in my example case, I used on the child express app, using the following function:


  app.use(function(req, res, next){
    res.send('hello world');
    next();
  });


It appears the parent app neither provides request communications to child express apps, or the child express apps aren't communicating to the local host...minding I did set up a listener on the child express app


if (!module.parent) {
  app.listen(3000, function(){
    console.log("Express server listening on port %d in %s mode",   app.address().port, app.settings.env);
  });
}


Thus getting a response Cannot GET /childappuri/ when the uri    localhost:3000/childappuri/ is called

At this point stumped what were occurring here.  Set up the basic of examples here again...otherwise, the workaround were embedding much in the main app all of the process (a little more involved).  Hoping I could find a working example on the  most current express 3 version, but couldn't aside from already provided advice through common forums such as StackOverflow which were general enough, didn't see anything at the moment.


A solution:
Thanks to another posting in StackOverflow.  The simplest that I've found is simply exporting the app.
Thus I added after all configurations and routing adds the following in the body of child express app in the app.js file:

module.exports = app;

All references above in the parent app should be used, and make sure that the server instantiation in the child express app is removed while using a listener (hadn't checked to see if this were technically necessary).

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.

Tuesday, November 6, 2012

Electoral outcomes python script


The following python scripts computes a permutation of outcomes for electoral success if Obama holds leaning blue states.  I computed a total of 18 distinct electoral outcomes which lead to success, this actually could be reduced a bit, since there maybe set overlap if not including the state of Florida which automatically provides Obama a win if Obama holds every other projected state and leaning states are included.  Just run script in Idle then type 'perms' or you could modify script for a list print

mapdict = {'va': 13, 'nh': 4, 'oh': 18, 'wi': 10, 'ia': 6, 'co': 9}

def checkset(pickmap, mappicks):
    mpcheck = True
    checkcont = 0
    for mappick in mappicks:
        checkcont = 0
        for pick in pickmap:
            if pick in mappick:
                checkcont += 1
            if checkcont == len(mappick):
                return True
 

def checkmap(inmap, score):
    mappicks = {}
    mappick = []
    newscore = score
    for state in inmap:
        newscore = score
        newscore += mapdict[state]
     
        mapcopy = inmap.copy()
        del mapcopy[state]
        if newscore < 27:
            #print(state, len(mapcopy))
            returnpicks = checkmap(mapcopy, newscore)
         
            for pick in returnpicks:
                #print (state, pick)
                if returnpicks[pick] > 26:
                    cpick = list(pick)
                    rpick = cpick[0:len(cpick)]
                    rpick.append(state)
                    rtuppick = tuple(rpick)
                    if not checkset(rtuppick, mappicks):
                        mappicks[rtuppick] = returnpicks[pick]
         
        else:
            mappicks[tuple([state])] = newscore
    return mappicks


perms = checkmap(mapdict, 0)

Oblivion

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