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

look into passing next into each function #15

Open
jonathanong opened this issue Feb 26, 2016 · 2 comments
Open

look into passing next into each function #15

jonathanong opened this issue Feb 26, 2016 · 2 comments

Comments

@jonathanong
Copy link
Member

like regular middleware. might be needed to support #14

still think it's an anti-pattern MOST of the time since your logic should be refactored into functions, not middleware.

@ifraixedes
Copy link
Contributor

@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?

@jonathanong
Copy link
Member Author

@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

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

2 participants