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

express-promise-handler

v3.3.1

Published

allows express middle-wares to return promise

Downloads

1,507

Readme

express-promise-handler

Allows express middlewares to return promise by wrapping the middlewares.

  • Makes express route handlers testable by avoiding use of res.
  • Slightly easier to work with promises.

Objective

Make the express route controllers easy to test and easy to read

Concepts

  • return from the callback is sent as response
  • object based responses are returned as json
  • thrown errors are caught and handled with 500
  • custom error can be thrown using HTTPError
  • returning undefined will call the next method. If there is no next, this can result in 504 timeout.

Example

Setting up routes

let ContentController = require('./content.controller');
let Router = require('express').Router;
let controller = new ContentController();
let router = new Router();

let promiseHandler = require('express-promise-handler').default;

router.get('/:id', promiseHandler(controller.info));

module.exports = router;

In the controller

const HTTPError = require('express-promise-handler').HTTPError;

class ContentController {
  info(req) {
    return models.content.findOne({
      where: {
        id: req.params.id
      }
    })
    .then((entry) => {
      if (!entry) {
        throw new HTTPError(404, 'entry not found');
      }
      return entry;
    });
  }
}

exports.default = ContentController;

Sample response

{
  "message": "entry not found"
}
{
  "message": "Internal server error: cbnvdtbz51v9pc8h395qd"
}

Error tracking

Thrown errors which are not instances of HTTPError are logged with full stack traces. These stack traces are assigned a unique alphanumeric string which is also sent as part of the response. API clients can choose to display this string to the users in order to track individual issues.

This also ensures that uncaught errors will not have any details conveyed in the response.

Changelog

3.3.1 2023-12-23

  • Fix the type check for HTTPError using instanceof instead of constructor name. It was causing issues when used with got where the error is also named HTTPError.

[3.3.0] 2023-10-31

Changed

  • Added support for complex error object with more properties

[3.2.0] - 2023-10-21

Changed

  • Added typescript definitions for subscribeError callback

[3.1.0] - 2023-10-09

Changed

  • added typescript support
  • Response will be set as second argument to use for setting headers
  • added package-lock and removed yarn.lock

[3.0.0] - 2020-04-09

Changed

  • send assertion error as http status 400 with the given message

[2.1.0] - 2017-12-23

Changed

  • added feature to handle buffer data as raw

[2.0.0] - 2017-09-24

Changed

  • Error in now always json
  • HTTPError is considered caught and not logged in detail
  • HTTPError.message is now a json object
  • handle next() methods in middleware

Alternative:

wrapper for Express 4's Router that allows middleware to return promises