https://github.com/amkjs/amk-wrap
function wrapper to catch errors in express.js controllers
https://github.com/amkjs/amk-wrap
amkjs error-handler error-wrapping express
Last synced: 8 months ago
JSON representation
function wrapper to catch errors in express.js controllers
- Host: GitHub
- URL: https://github.com/amkjs/amk-wrap
- Owner: amkjs
- License: mit
- Created: 2017-08-23T02:14:21.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-03-13T07:29:26.000Z (almost 2 years ago)
- Last Synced: 2025-04-13T12:39:56.446Z (9 months ago)
- Topics: amkjs, error-handler, error-wrapping, express
- Language: JavaScript
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AMK-WRAP
[](https://github.com/amkjs/amk-wrap/actions/workflows/node.js.yml)
[](https://codecov.io/github/amkjs/amk-wrap)
[](https://snyk.io/test/github/amkjs/amk-wrap?targetFile=package.json)
function wrapper to catch errors in [express](https://expressjs.com/) controllers
## Usage
to install: `npm i amk-wrap`
can pass a function or a class method.
on the router file:
```
const express = require('express');
const router = express.Router();
const wrap = require('amk-wrap');
class Controller {
constructor() {
this.something = "something"
}
async get(req, res) {
this.something; // access this
res.send('hello world');
}
}
const controller = new Controller();
module.exports = function (controller) {
router.get('/', wrap(controller, 'get')); // pass a class method
return router;
}
```
on the index.js:
```
const wrap = require('amk-wrap');
// some other code
app.get('/', wrap(getFunction)); // pass a function
```
or
```
const wrap = require('amk-wrap');
// some other code
app.get('/', wrap((req, res) => {
res.send('hello world');
}));
```
## Tests
1. install dependencies using `npm install`
2. run `npm test`
## Feedback
All bugs, feature requests, pull requests, feedback, etc., are welcome. [Create an issue](https://github.com/amkjs/amk-wrap/issues).
## License
[MIT](https://github.com/amkjs/amk-wrap/blob/master/LICENSE)