https://github.com/bcomnes/p-connect
https://github.com/bcomnes/p-connect
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bcomnes/p-connect
- Owner: bcomnes
- License: mit
- Created: 2019-07-29T16:11:01.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-24T14:16:37.000Z (over 2 years ago)
- Last Synced: 2025-05-24T00:50:09.482Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 48.8 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# p-connect
Converts connect style middleware, routes, errorRoutes to a promise. Also supports `http-hash-route` style routes.
```console
npm install p-connect
```
## Usage
``` js
const { pMiddleware, route } = require('p-connect')
const bodyParser = require('body-parser')
const pJson = pMiddleware(bodyParser.json())
const asyncRoute = async (req, res) => {
await pJson(req, res)
// req.body is now populated
}
app.use(route(asyncRoute))
```
## API
### `pMiddleware(connectMiddleware)`
Convert a callback style middleware to work inside of an asynchronous environment. Does not return any value, it will just throw if an error occures inside the middleware.
### `route(asyncConnectRoute)`
Converts an async connect route to a callback connect route.
`connectRoute(req, res, next)` should return a promise. `route(connectRoute) = (req, res, next) => {...}` returns a connect style callback middleware that calls next(err) when asyncConnectRoute throws. You should still call the `next` callback in async middleware if you want to keep the middleware chain moving.
## License
MIT