-
Notifications
You must be signed in to change notification settings - Fork 51
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
Context's app reference #25
Comments
koa and the official middleware do not consider apps self-contained. always consider i don't think this is going to change. |
I see. What is then the idea behind modular, distributable apps? This would essentially mean that an app augmented with functionality which is relied upon in its routes would cease to function once mounted. |
this isn't a philosophical issue, purely implementational. we're just not sure how to implement this correctly, concisely, and make everyone happy, so we didn't bother at all. |
Ah, I'm sorry I misunderstood. I did give the following a shot locally, which seems to work for the setup I had: diff --git a/index.js b/index.js
index 553260e..60acccc 100644
--- a/index.js
+++ b/index.js
@@ -47,21 +47,26 @@ function mount(prefix, app) {
return function *(upstream){
var prev = this.path;
+ var prevApp = this.app;
var newPath = match(prev);
debug('mount %s %s -> %s', prefix, name, newPath);
if (!newPath) return yield* upstream;
this.mountPath = prefix;
this.path = newPath;
+ this.app = app;
debug('enter %s -> %s', prev, this.path);
yield* downstream.call(this, function *(){
this.path = prev;
+ this.app = prevApp;
yield* upstream;
+ this.app = app;
this.path = newPath;
}.call(this));
debug('leave %s -> %s', prev, this.path);
+ this.app = prevApp;
this.path = prev;
} I'd gladly open a pull request if this is something to have in the main repo. |
the problem is that you'll run into other issues like passing settings through each app. you don't want 100% encapsulation, but you also don't want 0%. if you have an idea of exactly how encapsulation would work, that would be great! |
Hm, good point. Currently, a mounted app that relies on settings through the context would need its settings to be applied to the root application, right? What kind of settings would one like to share? |
this is the problem. people want different settings to be passed based on their app. |
Wouldn't it then be more correct and flexible to have settings separated and allow developers themselves to configure how they see fit? That would mean in certain circumstances having to configure certain settings multiple times (e.g. custom |
This issue could be solved by creating an new context instead of using the parent's context: yield* downstream.call(this, function *(){ after: var context = app.creatContext(this.req, this.res);
yield* downstream.call(context, function *(){ So that related issue: #5. @jonathanong The current API( |
really hard trap.... koa = require 'koa'
mount = require 'koa-mount'
error = require 'koa-error'
rootApp = koa()
subApp = koa()
rootApp.use error()
rootApp.context.rofl = "Root not Groot"
subApp.context.rofl = "Sub?"
subApp.use (next) ->
this.body = this.app.context.rofl
yield next
rootApp.use mount subApp
rootApp.listen process.env.PORT, process.env.IP, (err) =>
throw err if err
console.log "Server running on #{process.env.IP}:#{process.env.PORT}" this prints Root not Groot But what if i put (like koajs documentation) the db to my context? Example from koajs.com
app.context.db = db(); the mounting of koa apps means - mounting an encapsulated application - every other meaning make no sense - like this example... koa = require 'koa'
mount = require 'koa-mount'
error = require 'koa-error'
rootApp = koa()
rootApp.context.db = rootDB()
rootApp.use error()
blogApp = koa()
blogApp.context.db = blogDB()
blogApp.use (next) ->
results = yield @app.context.db().posts.sort('-createdBy').limit(10).exec()
@state.latestPosts = results
yield next
rootApp.use mount '/blog', blogApp
rootApp.listen process.env.PORT, process.env.IP, (err) =>
throw err if err
console.log "Server running on #{process.env.IP}:#{process.env.PORT}" what could be a solution?
koa = require 'koa'
mount = require 'koa-mount'
error = require 'koa-error'
_ = require 'lodash'
rootApp = koa()
rootApp.context.db = rootDB()
rootApp.use error()
blogApp = koa()
blogApp.on 'mount', (parent) ->
_.merge blogApp.context.config, parent.context.config
blogApp.context.db = blogDB blogApp.context.config.blog
# ...
rootApp.use mount '/blog', blogApp
rootApp.listen process.env.PORT, process.env.IP, (err) =>
throw err if err
console.log "Server running on #{process.env.IP}:#{process.env.PORT}"
This is no solution...module.exports = (subApp) ->
return (next) ->
this.app.context.db = subApp.context.db
yield next |
Had expected the above to work, but it raises. Should the app reference in the context refer to the main/base app or the currently handling app?
The text was updated successfully, but these errors were encountered: