Open jonathanong opened 8 years ago
@jonathanong my understanding is that you would like that "route" functions signature only receive the context (ctx
) and the next one in the list, are called when the previous returns;
Silly example:
function call1(ctx) {
ctx.body = ['called-1']
}
function call2(ctx) {
ctx.body.push('called-2')
}
function call3(ctx) {
ctx.body.push('called-3')
ctx.body = ctx.body.join(':')
}
const app = new Koa()
app.use(match('/a/b', [call1, call2, call3]))
// Response body should be a string with value 'called1:called2:called3'
when I look to that, I see it's neater, however I wouldn't like to miss the possibility to have middlewares only in some routes, for example
const bodyParser = require('koa-bodyparser')
const pathMatch = require('koa-path-match')
...
function loginPage(ctx) {
// Render login page
}
function verifyLogin(ctx) {
// do things to validate and responds
}
const match = pathMatch()
const parser = bodyParser()
const app = new Koa()
app.use(match('/login')
.get(loginPage)
.post([parser, verifyLogin])
If we don't pass next
to the route functions then bodyParser
cannot be used only in the post
verb, I would have to add as a middleware before match
middleware. Correct?
@ifraixedes correct. also, I don't recommend using a middleware for body parsing - with async functions, you can parse it on-demand https://www.npmjs.com/package/koa-body-parsers
like regular middleware. might be needed to support https://github.com/koajs/path-match/issues/14
still think it's an anti-pattern MOST of the time since your logic should be refactored into functions, not middleware.