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

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