boomware

1.1.3 • Public • Published

boomware

Opinionated Boom-based wrapper for asynchronous middleware (or handler) for Express, Connect, router, etc.

Installing

npm i --save boomware

Example

const boom = require('boom')
const boomware = require('boomware')
const express = require('express')
 
const app = express()
 
// Middleware can return a Promise (but it doesn't have to)
app.get('/async', boomware(async (req, res) => {
  if (Math.random() > 0.75) throw('Unexpected error!')
  if (Math.random() > 0.5) throw boom.serverUnavailable()
  res.send('OK!')
}))
 
app.use((err, req, res, next) => {
  console.error(err)
  res.status(err.output.statusCode).json(err.output.payload)
})
 
app.listen(3000)

API

boomware(fn)

Returns a middleware function that catches a thrown error, wraps it in an Boom.badImplementation if it's not already a Boom error, and passes it to the next() function. boomware handles the case whenfn return Promise, and the case when fn does not return a Promise.

Related work

License

MIT

Package Sidebar

Install

npm i boomware

Weekly Downloads

1

Version

1.1.3

License

MIT

Unpacked Size

4.74 kB

Total Files

5

Last publish

Collaborators

  • sbw