Home

Awesome

express-debug-async-wrap npm npm JavaScript Style Guide npm

express async wrapper that passes custom debug instance or logging function

Install

npm i -S express-debug-async-wrap
or
npm install --save express-debug-async-wrap

Use

In your route: Require and initialize with debug instance:

const debug = require('debug')('myNamespace:myRouteName')
const wrapper = require(`express-debug-async-wrap`)(debug)
const express = require('express')

const router = express.Router()

router.get('/', wrapper(async (req, res) => {
  await ...
  await ...
  res.send('OK')
}))

module.exports = router

In your main app:

const debug = require('debug')('myNamespace')
const express = require('express')
const app = express()
// error handler
app.use((err, req, res, next) => {
  err.status = err.status || 500
  let customDebug = debug
  if (err.debug) {
    customDebug = err.debug
    delete err.debug
  }
  if (err.status === 404) delete err.stack // Do not show error stack for 404's
  customDebug(err)

  res.status(err.status)
  res.json(err) // For JSON APIs
  // res.send(err) // Or send as text
})

Related

License

MIT Copyright © Diego Rodríguez Baquero