conditionally app.use() according to request parameter

1,580 views
Skip to first unread message

Michael Derazon

unread,
Jan 21, 2014, 7:33:16 PM1/21/14
to expre...@googlegroups.com
Hi, I have two middlewares created by express() function :

// module_a.js 
var express = require('express')
app = express();
...
module.exports = app

// module_b.js 
var express = require('express')
app = express();
...
module.exports = app

now, in the main module, I want to use only one of the middlewares, based on a query parameter. Something like this:

// main.js
app.use(function(req, res) {
  if (req.query.something) {
    app.use(module_a); // and pass req, res somehow to that middleware
  } else {
    app.use(module_b) // and pass req, res somehow to that middleware
  }
});

since the result of express() is an object, as opposed to a function usually passed to app.use() I have no way of doing it.

Any idea ?


Thanks

Hage Yaapa

unread,
Jan 21, 2014, 11:10:22 PM1/21/14
to expre...@googlegroups.com
You are basically mounting apps in your main app; req and res will be passed to the sub app by default. However, it looks like the problem might be something else, you might want to rethink the architecture, it seems unnecessarily complicated.


--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/groups/opt_out.

greelgorke

unread,
Jan 22, 2014, 3:48:02 AM1/22/14
to expre...@googlegroups.com
first of all: a middleware is just a function, witch takes a request, a response, a next callback. what you are doing are express apps. they are complete apps, whcih come with its own router and stuff.

query param is usually not the right place for routing informations. you would be better with converting it to path param, then you can just mount your apps under the routes

but if you must, thenit would be better to check for the param in the subapp and call next if it doesnt match.

Michael Derazon

unread,
Jan 22, 2014, 10:14:30 AM1/22/14
to expre...@googlegroups.com
The reason I am doing it is because I want to start versioning my api, I am actually putting the version inside the header (not the query) but it's the same. I could have created different route for each api version, but this changes my API spec.

I could check for the version in each of the sub apps routes and hit "next" if it's not the correct version, just seems like unnecessary amount of code

Thanks

greelgorke

unread,
Jan 22, 2014, 11:05:26 AM1/22/14
to expre...@googlegroups.com
if you start with the versioning than its a good thing to add the version path param to you new api. it's kinda standard and more obvious than a header param. but sure, it's your api
Reply all
Reply to author
Forward
0 new messages