Connect: Routing

61 views
Skip to first unread message

Kevin Ingwersen (Ingwie Phoenix)

unread,
Jan 21, 2015, 9:01:01 PM1/21/15
to nod...@googlegroups.com
Hey folks.

I am trying to work out my way aorund a tiny routing issue I have.

There are three major modules: API, CDN and Main. I want to map them as follows:

main => /
cdn => /cdn
api => /api

So anything that goes to /api/* is handled by the API part, for instance. But all that is called is the main route, and the other two just never get a chance.

What would the proper connect setup be to use all three routes? Because I just can’t seem to get all three working at the same time…

API parses the URL into an API call and sends back a JSON object.
CDN is a static, caching, gzipping file server.
Main is a dynamic content generating part.


Kind regards,
Ingwie

Oleg Verych

unread,
Jan 22, 2015, 2:25:29 AM1/22/15
to nod...@googlegroups.com
Hey folks.


Hi.
 
I am trying to work out my way aorund a tiny routing issue I have.

There are three major modules: API, CDN and Main. I want to map them as follows:

main => /
cdn => /cdn
api => /api

So anything that goes to /api/* is handled by the API part, for instance. But all that is called is the main route, and the other two just never get a chance.

What would the proper connect setup be to use all three routes? Because I just can’t seem to get all three working at the same time…


Some code would be nice to look at.

Connect as simple http server may have request handlers like so:

  var server = require('http').createServer(router);
 
 
function router(req, res){
    console
.log(req.url);

   
if('/' == req.url){
     
return main(req, res);
   
}
   
if('/cdn' == req.url.slice(0, 4)){
     
return cdn(req, res);
   
}
   
return api(req, res);
 
}


But for handling middleware stack any such function has 3d argument

  var connect = require('connect')
 
var app = connect()

  app
.use(mwRouter)

 
function mwRouter(req, res, next){
   
var url;

   
if('/' == req.url){
     
return main(req, res, next);
   
}
    url
= req.url.slice(0, 4);
   
   
if('/cdn' == url){
     
return cdn(req, res, next);
   
}
   
if('/api' == url){
     
return api(req, res, next);
   
}
   
   
return next();// go on with other middlewares
 
}


 `next()` is next handler installed by app.use(function handler(res, req, next){ }) or this is a shortcut to an error handler if `next(error)` was actually called.

-- 
sed 'sh && sed && node.js + olecom = happiness and mirth'  <<  ''
-o--=O`C
 #oo'L O
<___=E M

Kevin Ingwersen (Ingwie Phoenix)

unread,
Jan 22, 2015, 11:05:04 PM1/22/15
to nod...@googlegroups.com
Right, should’ve shown the code beforehand. Here goes:


Please note that I have to use vhost for this case. For now, that seems to somehow work. But. If I pass a new connect instance to the api_handler.js file, it will never be reached. So I think my routing is a little f****d up…

When I tried to turn this whole thing into a clustered aproach (in hopes to increase speed, which seemed to not have been the case…) I had tried to make the orutes without the vhost module at all, which then looked like:

var connect = require("connect"),
    app = connect(),
    main = connect(),
    api = connect(),
    cdn = connect();

app.use(connectLogger());
app.use(responsetime());
app.use(compress());

// Now comes
app.use("/", main);
app.use("/cdn", cdn);
app.use("/api", api);

// The files were re-written by then.
require("lib/request_handler")(main);
require("lib/cdn_handler")(cdn);
require("lib/api_handler")(api);

But that resulted in a total mess. My main site loaded, but only the / route got hit - none of the others were! Even when I moved it to the bottom, same result…
-- 
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cdc1ca19-0717-444c-b9d5-97a98f96fba1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kris De Volder

unread,
Feb 1, 2015, 4:03:55 PM2/1/15
to nod...@googlegroups.com
The order matters when you call 'app.use(..)'.
Try changing the order of your calls so that app.use('/', ...) is the last one instead of the first one.

Kris

Reply all
Reply to author
Forward
0 new messages