Created
May 28, 2016 04:46
-
-
Save thejmazz/3cb205f786c9cd03f24d0890f9114ef2 to your computer and use it in GitHub Desktop.
Express style middleware callstack with koa-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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) |
Author
thejmazz
commented
May 28, 2016
•
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