blakeembrey / compose-middleware

Compose an array of middleware into a single function for use in Express, Connect, router, etc
MIT License
91 stars 8 forks source link

How to use with http.createServer? #18

Open matthewmueller opened 5 years ago

matthewmueller commented 5 years ago

This seems to work, but I'm not sure if I'm missing anything (like error handling):

const { compose } = require('compose-middleware')
const { createServer } = require('http')

const middleware = []
const handler = compose(middleware)

const server = createServer((req, res) => {
  handler(req, res, () => res.end())
})

Thanks for creating this package @blakeembrey!

blakeembrey commented 5 years ago

@matthewmueller This is fine. You could also use finalhandler like Express/Connect does. Error handling is supported by the four argument syntax and passes it via next(err) (same as Express/Connect also). Would love to use this as reference for updating the README, is that alright?