Skip to content

Instantly share code, notes, and snippets.

@yoshuawuyts
Last active August 27, 2015 18:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshuawuyts/42a4c86e7041569b7051 to your computer and use it in GitHub Desktop.
Save yoshuawuyts/42a4c86e7041569b7051 to your computer and use it in GitHub Desktop.
const sequence = require('run-sequence')
// create a middleware stack
// (obj, obj, [fn(obj, obj, fn)], fn) -> null
function mw (req, res, arr, done) {
const fns = arr.map(fn => (next) => fn(req, res, next))
sequence(fns, done)
}
@yoshuawuyts
Copy link
Author

Usage:

require('http').createServer((req, res) => {
  const fns = [
    (req, res, next) => { 
      res.write('oh yeah!')
      next()
    },
    (req, res, next) => { 
      res.statusCode = 200
      next()
    }
  ]
  require('./middleware')(req, res, fns, res.end)
}).listen(1337)

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