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

use path-to-regex for express-like mounted routes #14

Open
jonathanong opened this issue Mar 25, 2014 · 8 comments
Open

use path-to-regex for express-like mounted routes #14

jonathanong opened this issue Mar 25, 2014 · 8 comments

Comments

@jonathanong
Copy link
Member

app.use(mount('/:id(\d+)', fn))

interested? path-to-regex does prefix routing. should be backwards compatible. not sure how i want to handle keys though. maybe (next, keys...)

@jonathanong jonathanong changed the title mounting with "routes" mounting with "routes" or "params" Mar 25, 2014
@jonathanong jonathanong changed the title mounting with "routes" or "params" use path-to-regex for express-like mounted routes Mar 25, 2014
@tj
Copy link
Member

tj commented Mar 25, 2014

I can't think of too many cases where it would be useful, usually you can just do the same thing by rewriting the url to match the mount point, but I think even just concept of mounting being just like the mount command is nice, not sure that we need regexp support, the middleware wouldn't know how to act on the params

@travisjeffery
Copy link
Member

this would be useful for nested resources. e.g. you have users that have posts and routes like:

/users/:id/posts/:id

and you wanna have two apps: a users app and a posts app - where the posts app is mounted in the users app.

there's no good way to do this currently (i think?) since the posts app would be mounted at /:id/posts.

@bilalaslam
Copy link

👍 on this request

@matthewmueller
Copy link

+1

1 similar comment
@lazdmx
Copy link

lazdmx commented Mar 31, 2015

👍

@romeovs
Copy link

romeovs commented Dec 12, 2015

I'm also for this!
I often need to compose several sub-applications where one sub-application just needs to take all the paths that are not covered by the other ones. But I do want to have some shared things also.

Example:

import Koa   from 'koa'
import mount from 'koa-mount'

import api    from './subapps/api'
import html   from './subapps/html'
import logger from './tools/logger'

const app = new Koa();

// api is mounted under api (this is possible)
app.use(mount('/api', api));

// html is mounted at /, but isn't called when the path starts with /api
// this is currently not possible
app.use(mount(/^\/(?!api)/, html);  

// shared stuff
app.use(logger);

app.listen(3000);

I hope you see the validness of this use-case?

There's a workaround i'm currently using, but it is ugly.
I just put this on top of every handler in the html route:

html.use(function (ctx, next) {
  if (/^\/(api)/.test(ctx.url)) { return next(); }
  //... 
});

But this is becoming very tedious to maintain. It'd be better to have all the paths in one location.

@anupnitkkr
Copy link

+1

@anupnitkkr
Copy link

with different baseurl, I need to add all route with this mount method

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

8 participants