Giter VIP home page Giter VIP logo

Comments (8)

romeovs avatar romeovs commented on July 20, 2024 3

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.

from mount.

tj avatar tj commented on July 20, 2024

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

from mount.

travisjeffery avatar travisjeffery commented on July 20, 2024

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.

from mount.

bilalaslam avatar bilalaslam commented on July 20, 2024

👍 on this request

from mount.

matthewmueller avatar matthewmueller commented on July 20, 2024

+1

from mount.

lazdmx avatar lazdmx commented on July 20, 2024

👍

from mount.

anupnitkkr avatar anupnitkkr commented on July 20, 2024

+1

from mount.

anupnitkkr avatar anupnitkkr commented on July 20, 2024

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

from mount.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.