Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/diegorbaquero/express-debug-async-wrap
express async wrapper that passes custom debug instance
https://github.com/diegorbaquero/express-debug-async-wrap
async debug express express-async-wrapper expressjs wrap wrapper
Last synced: 14 days ago
JSON representation
express async wrapper that passes custom debug instance
- Host: GitHub
- URL: https://github.com/diegorbaquero/express-debug-async-wrap
- Owner: DiegoRBaquero
- License: mit
- Created: 2017-12-31T17:27:30.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-29T00:57:47.000Z (about 4 years ago)
- Last Synced: 2025-01-05T07:10:52.153Z (17 days ago)
- Topics: async, debug, express, express-async-wrapper, expressjs, wrap, wrapper
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 2
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# express-debug-async-wrap [![npm](https://img.shields.io/npm/v/express-debug-async-wrap.svg?style=flat-square)]() [![npm](https://img.shields.io/npm/dm/express-debug-async-wrap.svg?style=flat-square)]() [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](https://standardjs.com) [![npm](https://img.shields.io/npm/l/express-debug-async-wrap.svg?style=flat-square)](LICENSE)
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:
```js
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:
```js
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
- [express-route-autoloader](https://github.com/DiegoRBaquero/express-route-autoloader)
- [express-sequelize-crud-router](https://github.com/DiegoRBaquero/express-sequelize-crud-router)
- [sequelize-express-findbyid](https://github.com/DiegoRBaquero/sequelize-express-findbyid)## License
MIT Copyright © [Diego Rodríguez Baquero](https://diegorbaquero.com)