npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@toanerrors/async-middleware

v1.0.5

Published

Async middleware for Express

Downloads

3

Readme

@toanerrors/async-middleware

Async middleware for Express

Table of Contents

Introduction

@toanerrors/async-middleware is a lightweight library that enhances Express applications with support for asynchronous middleware. This library wraps asynchronous request handlers to ensure that errors are properly passed to Express's error handling middleware.

Installation

You can install this library using npm:

npm install @toanerrors/async-middleware

or using yarn:

yarn add @toanerrors/async-middleware

or using bun:

bun add @toanerrors/async-middleware

Usage

To use @toanerrors/async-middleware, simply import the asyncMiddleware function and apply it to your Express application. This will enhance the specified HTTP methods to support asynchronous request handlers.

import express from "express";
import { asyncMiddleware } from "@toanerrors/async-middleware";

const app = express();

// Apply asyncMiddleware
asyncMiddleware(app);

// If you have routes that need to be enhanced with asyncMiddleware
const router = express.Router();
// Apply asyncMiddleware to the router
asyncMiddleware(router);

app.get("/", async (req, res) => {
  // Your asynchronous code here
  throw new Error("Something went wrong");
});

// Error-handling declare once
app.use((err, req, res, next) => {
  res.status(500).send(err.message);
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

using with express.Router

import express from "express";
import { asyncMiddleware } from "@toanerrors/async-middleware";

const router = express.Router();

asyncMiddleware(router);

router.get("/", async (req, res) => {
  // Your asynchronous code here
  throw new Error("Something went wrong");
});

// If you have already defined an error-handling function in the main file, you do not need to define it within the router.

export default router;

Configuration

The asyncMiddleware function accepts an optional configuration object that allows you to specify which HTTP methods should be enhanced with asynchronous support. By default, it supports the following methods: get, post, put, delete, and patch.

import express from "express";
import { asyncMiddleware } from "@toanerrors/async-middleware";

const app = express();
asyncMiddleware(app, {
  methods: ["get", "post"], // Only enhance GET and POST methods
});

app.get("/", async (req, res) => {
  res.send("Hello World");
});

app.post("/data", async (req, res) => {
  res.send("Data received");
});

app.use((err, req, res, next) => {
  res.status(500).send(err.message);
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Examples

// file: index.ts
import express from "express";
import { asyncMiddleware } from "@toanerrors/async-middleware";
import router from "./router";

const app = express();
asyncMiddleware(app);

app.get("/error", async (req, res) => {
  throw new Error("Test error");
});

app.use("/router", router);

app.use((err, req, res, next) => {
  res.status(500).send(err.message);
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});
// file: router.ts
import express from "express";
import { asyncMiddleware } from "@toanerrors/async-middleware";

const router = express.Router();

asyncMiddleware(router);

router.get("/error", async (req, res) => {
  throw new Error("Test error");
});

export default router;

Using with Multiple Handlers

import express from "express";
import { asyncMiddleware } from "@toanerrors/async-middleware";

const app = express();
asyncMiddleware(app);

const firstHandler = async (req, res, next) => {
  console.log("First handler");
  next();
};

const secondHandler = async (req, res) => {
  res.send("Second handler");
};

app.get("/", firstHandler, secondHandler);

app.use((err, req, res, next) => {
  res.status(500).send(err.message);
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Contributing

Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.

License

MIT License Copyright (c) 2024 toanerrors