compose-middleware icon indicating copy to clipboard operation
compose-middleware copied to clipboard

How to use with http.createServer?

Open matthewmueller opened this issue 6 years ago • 1 comments

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!

matthewmueller avatar Jan 31 '19 05:01 matthewmueller

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

blakeembrey avatar May 26 '19 19:05 blakeembrey