Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mounting sub apps with a route prefix doesn't support :params #1881

Closed
pwmckenna opened this issue Dec 31, 2013 · 6 comments
Closed

mounting sub apps with a route prefix doesn't support :params #1881

pwmckenna opened this issue Dec 31, 2013 · 6 comments

Comments

@pwmckenna
Copy link
Contributor

var express = require('express');
var userApp = express();
userApp.get('/', function (req, res) {
    res.send(200);
});

var app = express();
app.use('/:user', userApp);
app.listen(80);

localhost/:user -> 200
localhost/actualUserName -> 404

@tj
Copy link
Member

tj commented Dec 31, 2013

yeah right now mounting is a different concept, more like mount(1), so it doesn't have routing features, but they're also used for different reasons. You could do similar with app.all(), or just app.use(userApp) and have .get('/:user') in there

mounting is basically meant for "hiding" part of the part from middleware

@pwmckenna
Copy link
Contributor Author

app.all should work perfectly. i was trying to create a situation where the user subapp could always assume that req.user was available (and the resource app could always assume req.resource, etc) without introducing per-route middleware. I assume that use('/asdf') is much more efficient than all('/asdf*') correct? also, is anyone maintaining express now that koa is released?

@pwmckenna
Copy link
Contributor Author

the one significant difference i'm noticing with all is that the req.url that the route handler received doesn't have the portion of the route that matched the mounted prefix trimmed off (/:user in this case). i'm a little hesitant to alter the req object directly...do you have any ideas of how to pass an altered copy of the req object to the all middleware? I assume that calling next(newReqObj, res) won't have the desired effect.

@tj
Copy link
Member

tj commented Dec 31, 2013

nah they're more or less the same efficiency-wise, I wouldn't worry about that. Yup there's 3 or 4 maintainers. nothing wrong with rewriting req.url though, that's what mounting does, for logging etc there's req.originalUrl, node doesn't really facilitate creating mock request objects

@pwmckenna
Copy link
Contributor Author

k, i'll give that a try. thanks!

@Prinzhorn
Copy link

I have a very similar question. I'm looking for the best way to have the same routes under different language folders. E.g. /en/users and /de/users. https://github.com/visionmedia/express-namespace looks like what I need (namespacing to /:lang), but it's looking rather inactive. Also I'm starting a new project using express 4 and was hoping the new express.Router could make express-namespace obsolete.

I don't know if it has been discussed before, but how about matching the API returned from app.route with the router itself? What I mean is that app.route().get only expects a single function, whereas app.get also allows a path string. Ideally we could do the following in v4, which looks nice and makes express-namespace obsolete while making the API even more consistent.

var langRouter = app.route('/:lang');

//It's currently not possible to pass `/users` as parameter
langRouter.get('/users', function(req, res, next) {

});

Edit: Looks like #1935 fixes this, but with different api/logic though. I personally think it would be logical that app.router().get accepts the same parameters as app.get does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants