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-addons

v1.4.10

Published

Small addons for express.js.

Downloads

43

Readme

express-addons

Introduction

This is a small package for doing some addons on express.js 4.

Async Routes

Allows you to use async routes instead of callbacks.

Here is an example.

    route.get('/', async (req, res) => {
        
        if (!req.notAuthed) throw new Error('Not authorized.');
        
        await something();

        return 'This is my response.';
        
    });

It's simple and it's strait forward. Remark that the next parameter is not exposed to async routes.

| Non-async routes | Async routes | |:-----------|:-----| | next() | Exit scope | | next(new Error()) | throw new Error() | | send(data) | return data|

You can configure which response method should be used when you return data from the route to be send - see below.

Configuration

    const { asyncRoutes } = require('express-addons');
    
    asyncRoutes(/* { options } */);

Options

These options are available for the asyncRoutes addon.

| Name | Type | Description | Default | |:----|:-----|:------------|:--------| | autosender | String | The method on response to use when returning data from a route - e.g. send, json. | send | - or - | Function | A custom function to use (has prototype (data, req, res)).

Catch all

Catch all is a simple route, that you put at the end of your other routes to handle 405 Method Not Allowed.

Here is an example.

    route.get('/mypath/', ...);
    route.post('/mypath/', ...);
    route.delete('/mypath/', ...);
    route.catchAll('/mypath/');

In the above example the catchAll route returns 405 Method Not Found to the client and sets the Allow header.

You can also provide an additional routes for custom response handling.

    route.catchAll('/mypath/', (req, res) => {
        res.json({ error: 'method not allowed');
    });

Remark the first parameter is a path, which can be omitted and defaults to /.

Configuration

    const { catchAll } = require('express-addons');
    
    catchAll();

You can provide an overall additional route, which will be used on all instances where route.catchAll() is used by providing it with the configuration.

    const { catchAll } = require('express-addons');
    
    catchAll((req, res) => {
        res.json({ error: 'method not allowed' });
    });

If you also configured the async routes addon, you can also use async routes in the custom catchAll routes.

License

New BSD License. See file LICENSE.