Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Routing: the Right Way to Pass Application Data to Views and Route Names

Date: Mon, 10 Sep 2012 19:08:20 -0700 (PDT)
From: Jeff Schwartz <jefftschwa...@gmail.com>
To: express-js@googlegroups.com
Message-Id: <fe2958aa-9bb4-4dde-ae0f-2dc81dc3ef3f@googlegroups.com>
In-Reply-To: <59e870d2-e7e7-494c-add5-d8ba616e557c@googlegroups.com>
References: <b383fbd4-2e89-4bd7-90dc-41902a91dd1c@googlegroups.com>
 <fb4c6f13-24cf-4558-8f8d-1fc9e5d1e7ee@googlegroups.com>
 <59e870d2-e7e7-494c-add5-d8ba616e557c@googlegroups.com>
Subject: Re: Routing: the Right Way to Pass Application Data to Views and
 Route Names
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1789_20262184.1347329300541"

------=_Part_1789_20262184.1347329300541
Content-Type: multipart/alternative; 
	boundary="----=_Part_1790_17281930.1347329300541"

------=_Part_1790_17281930.1347329300541
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

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.

On Friday, September 7, 2012 2:20:31 PM UTC-4, Keith Rosenberg wrote:
>
> 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
>
> On Thursday, September 6, 2012 1:54:42 PM UTC-4, Jeff Schwartz wrote:
>>
>> 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 <http://expressjs.com/api.html#app.param>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!
>>>
>>>
------=_Part_1790_17281930.1347329300541
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

I've posted another article @&nbsp;http://jefftschwartz.wordpress.com/2012/=
09/10/taming-those-unruly-routes/. In it I discuss using CommonJs to reign =
in routing.<br><br>On Friday, September 7, 2012 2:20:31 PM UTC-4, Keith Ros=
enberg wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Jeff - that's an =
interesting idea. It feels like a lot of slinging objects back and forth bu=
t I like it. Will +1 your article<br><br>On Thursday, September 6, 2012 1:5=
4:42 PM UTC-4, Jeff Schwartz wrote:<blockquote class=3D"gmail_quote" style=
=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"=
>You can expose data and services to your routes through route-specific mid=
dleware. A while back I wrote an article on the subject which is located @&=
nbsp;<a href=3D"http://jefftschwartz.wordpress.com/2012/08/23/use-express-r=
oute-specific-middleware-to-access-mongodb/" target=3D"_blank">http://jefft=
schwartz.<wbr>wordpress.com/2012/08/23/use-<wbr>express-route-specific-<wbr=
>middleware-to-access-mongodb/</a>.<div><br></div><div>I use this widely in=
 my apps. It allows me to make my apps highly modularized while my modules =
remain loosely coupled.</div><div>&nbsp;<br></div><div><br>On Saturday, Sep=
tember 1, 2012 11:02:01 PM UTC-4, Keith Rosenberg wrote:<blockquote class=
=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc s=
olid;padding-left:1ex">The API documentation is pretty good as a reference,=
 but I could personally use a more comprehensive guide to routing in Expres=
s, or maybe even a generic routing standards and routing guidelines that sp=
ans across all server routing.<div><br></div><div>My situation is that I ha=
ve some routes in path "/routes/index.js" and they have dependancies on som=
e api requires in app.js, ala&nbsp;</div><div><br></div><div><br></div><div=
><font face=3D"courier new, monospace">--APP.JS--</font></div><div><font fa=
ce=3D"courier new, monospace">var core =3D require("/core").data;</font></d=
iv><div><font face=3D"courier new, monospace"><br></font></div><div><font f=
ace=3D"courier new, monospace">var app =3D express();</font></div><div><fon=
t face=3D"courier new, monospace">app.locals.core =3D core;</font></div><di=
v><font face=3D"courier new, monospace"><br></font></div><div><font face=3D=
"courier new, monospace">var routes =3D require('./routes')(app)<br></font>=
</div><div><font face=3D"courier new, monospace">----------</font></div><di=
v><font face=3D"courier new, monospace"><br></font></div><div><font face=3D=
"courier new, monospace">--ROUTES/INDEX.JS--</font></div><div><font face=3D=
"courier new, monospace"><div>module.exports =3D function(app){</div><div>&=
nbsp; &nbsp; // View list of core data</div><div>&nbsp; &nbsp; app.get('/co=
re', function(req, res){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; res.render('=
core', {&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; title: 'core' ,</div><=
div>&nbsp; &nbsp; &nbsp; &nbsp; core: app.locals.core</div><div>&nbsp; &nbs=
p; });</div><div><div>&nbsp; &nbsp; app.get('/' +<a href=3D"http://app.loca=
ls.user.id" target=3D"_blank">app.locals.user.id</a> + '&amp;uname=3D' + <a=
 href=3D"http://app.locals.user.name" target=3D"_blank">app.locals.user.nam=
e</a>, function(req, res){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; res.render=
('core', {&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; title: 'core' ,</div=
><div>&nbsp; &nbsp; &nbsp; &nbsp; core: app.locals.core,</div><div>&nbsp; &=
nbsp; &nbsp; &nbsp; user: app.locals.user</div><div>&nbsp; &nbsp; });</div>=
</div><div>});</div></font></div><div><font face=3D"courier new, monospace"=
>--------------------</font></div><div><br></div><div><font face=3D"arial, =
sans-serif">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 g=
et real messy with dynamic URLs and session-based objects being passed. I a=
m 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.</font></div><div><font fac=
e=3D"arial, sans-serif"><br></font></div><div><font face=3D"arial, sans-ser=
if">I saw this <a href=3D"http://expressjs.com/api.html#app.param" target=
=3D"_blank">app.param() entry</a> in the API and I was thinking it might be=
 party of my solution, but I don't think my core understanding of routing i=
s good enough to simply read the API doc and be like "Oh yeah I get it, sim=
ple". Anyone have any suggestions for keeping this clean, anything from gen=
eric routing guides I could read up on to specific examples you could share=
?</font></div><div><font face=3D"arial, sans-serif"><br></font></div><div><=
font face=3D"arial, sans-serif">Thanks much!</font></div><div><font face=3D=
"arial, sans-serif"><br></font></div></blockquote></div></blockquote></bloc=
kquote>
------=_Part_1790_17281930.1347329300541--

------=_Part_1789_20262184.1347329300541--