GithubHelp home page GithubHelp logo

diegorbaquero / express-debug-async-wrap Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 1.0 29 KB

express async wrapper that passes custom debug instance

License: MIT License

JavaScript 100.00%
express-async-wrapper express expressjs async wrapper wrap debug

express-debug-async-wrap's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.