Skip to content

Instantly share code, notes, and snippets.

@thejmazz
Created May 28, 2016 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejmazz/3cb205f786c9cd03f24d0890f9114ef2 to your computer and use it in GitHub Desktop.
Save thejmazz/3cb205f786c9cd03f24d0890f9114ef2 to your computer and use it in GitHub Desktop.
Express style middleware callstack with koa-compose
'use strict'
const koa = require('koa')
const compose = require('koa-compose')
const app = koa()
const first = function * (next) {
console.log('first')
yield next
}
const second = (foo) => {
return function * (next) {
console.log(`second with param ${foo}`)
yield next
}
}
const last = function * () {
this.body = 'Hello world\n'
}
app.use(compose([
first,
second('bar'),
last
]))
app.listen(3000)
@thejmazz
Copy link
Author

thejmazz commented May 28, 2016

koa-compose

@thejmazz
Copy link
Author

The Express equivalent would have been something like:

const express = require('express')
const app = express()

const first = function(req, res, next) {
  console.log('first')
  next()
}

const second = function(foo) {
  return function(req, res, next) {
    console.log(`second with param ${foo}`)
    next()
  }
}

const last = function(req, res) {
  res.send('Hello world\n')
}

app.use([first, second('bar'), last])

app.listen(3000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment