Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pacifiquem/node-async-handler

Async Error Handling Middleware for ExpressJS with ESM/TypeScript support.
https://github.com/pacifiquem/node-async-handler

async error error-handling express handling node nodejs try-catch

Last synced: 13 days ago
JSON representation

Async Error Handling Middleware for ExpressJS with ESM/TypeScript support.

Awesome Lists containing this project

README

        

# node-async-handler

Async Error Handling Middleware for Express with ESM Support

This middleware helps you eliminate the need for ```try-catch``` blocks throughout your Express application, streamlining error handling and improving code readability.

> **Note** : This is made specifically for ExpressJS projects.

## Usage

To use ```node-async-handler```, simply wrap your async functions in the asyncHandler middleware:

```ts
// For ESM
import pkg from 'node-async-handler';
const { asyncHandler } = pkg;

// For CommonJS
const { asyncHandler } = require('node-async-handler');

express.get('/', asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const result = await someAsyncFunction();
res.json(result);
}));
```

In case of an error, the middleware automatically calls ```next(error)```, allowing centralized error handling in your Express app.

## Importing

> **Note** : To import ```node-async-handler``` in ESM:
>
> ```ts
> import pkg from 'node-async-handler';
> const { asyncHandler } = pkg;
>```

> **Note** : To import ```node-async-handler``` in CommonJS:
>
> ```ts
> const { asyncHandler } = require('node-async-handler');
>```

## License

This project is licensed under the MIT License. See the [LICENSE file](https://github.com/pacifiquem/node-async-handler/blob/main/LICENSE) for details.

Author: MURANGWA Pacifique