Re: Routing: the Right Way to Pass Application Data to Views and Route Names

721 views
Skip to first unread message
Message has been deleted

Jeff Schwartz

unread,
Sep 6, 2012, 1:54:42 PM9/6/12
to expre...@googlegroups.com
You can expose data and services to your routes through route-specific middleware. A while back I wrote an article on the subject which is located @ http://jefftschwartz.wordpress.com/2012/08/23/use-express-route-specific-middleware-to-access-mongodb/.

I use this widely in my apps. It allows me to make my apps highly modularized while my modules remain loosely coupled.
 

On Saturday, September 1, 2012 11:02:01 PM UTC-4, Keith Rosenberg wrote:
The API documentation is pretty good as a reference, but I could personally use a more comprehensive guide to routing in Express, or maybe even a generic routing standards and routing guidelines that spans across all server routing.

My situation is that I have some routes in path "/routes/index.js" and they have dependancies on some api requires in app.js, ala 


--APP.JS--
var core = require("/core").data;

var app = express();
app.locals.core = core;

var routes = require('./routes')(app)
----------

--ROUTES/INDEX.JS--
module.exports = function(app){
    // View list of core data
    app.get('/core', function(req, res){
        res.render('core', { 
        title: 'core' ,
        core: app.locals.core
    });
    app.get('/' +app.locals.user.id + '&uname=' + app.locals.user.name, function(req, res){
        res.render('core', { 
        title: 'core' ,
        core: app.locals.core,
        user: app.locals.user
    });
});
--------------------

And then, of course "/core.ejs" references all the data in core. Is app.locals really the right way to do this? I think this is going to get real messy with dynamic URLs and session-based objects being passed. I am just trying to have some foresight and build something that won't require a complete overhaul in the future. As you can see in the second route, it's being built dynamically based on which user is loaded in this instance of the application, and it's starting to get ugly.

I saw this app.param() entry in the API and I was thinking it might be party of my solution, but I don't think my core understanding of routing is good enough to simply read the API doc and be like "Oh yeah I get it, simple". Anyone have any suggestions for keeping this clean, anything from generic routing guides I could read up on to specific examples you could share?

Thanks much!

Jeff Schwartz

unread,
Sep 6, 2012, 3:14:06 PM9/6/12
to expre...@googlegroups.com

Keith Rosenberg

unread,
Sep 7, 2012, 2:20:31 PM9/7/12
to expre...@googlegroups.com
Jeff - that's an interesting idea. It feels like a lot of slinging objects back and forth but I like it. Will +1 your article

Jeff Schwartz

unread,
Sep 8, 2012, 11:48:32 PM9/8/12
to expre...@googlegroups.com
Thanks, Keith. My example however isn't appropriate for anything but a smallish app with maybe a few routes at most. 

I planned to post a followup article where I would suggest what I believe to be a much better approach for apps that have to deal with dozens or even hundreds or even thousands of routes, such as what you might find with a restful api server. My approach is based on the common.js modular system and takes advantage of using folders as modules so you can encapsulate your routes completely outside of your main module.

Jeff Schwartz

unread,
Sep 10, 2012, 10:08:20 PM9/10/12
to expre...@googlegroups.com
I've posted another article @ http://jefftschwartz.wordpress.com/2012/09/10/taming-those-unruly-routes/. In it I discuss using CommonJs to reign in routing.
Reply all
Reply to author
Forward
0 new messages