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

micro-api-gateway

v8.3.1

Published

A small, flexible API gateway.

Downloads

13

Readme

micro-api-gateway

A basic API gateway implementation.

example

const micro_api_gateway = require( 'micro-api-gateway' );
const url_matcher = require( 'url-matcher' );

const gateway = micro_api_gateway.create();

gateway.init( {
    // you can define your own URL matching, for example, you could use
    // url-matcher to support things like /users/:user_id
    matcher: path => {
        return {
            match: url => {
                return url_matcher.getParams( path, url );
            }
        };
    },

    // you can define endpoints on the API gateway itself, eg:
    endpoints: [ { 
        methods: [ 'GET' ],
        path: '/version', // get the API gateway version
        handler: ( request, response ) => {
            const pkg = require( './package.json' );
            response.setHeader( 'content-type', 'application/json' );
            response.statusCode = 200;
            response.end( JSON.stringify( {
                version: pkg.version
            } ) );
        }
    }, {
        methods: [ 'GET' ],
        path: '/public.pem', // expose a public key used for signing requests
        handler: ( request, response ) => {
            response.setHeader( 'Content-Type', 'application/x-pem-file' );
            response.statusCode = httpstatuses.ok;
            response.end( PUBLIC_KEY );
        } )
    } ],

    // you can define global plugins for both endpoints and routes
    plugins: {

        // define global endpoint plugins
        endpoints: {

            // execute these before any endpoint-specific plugins
            pre: [
                // log requests
                require( 'micro-api-gateway/plugins/log' )()
            ],

            // execute these after any endpoint-specific plugins
            post: [

            ]
        },

        // define route plugins
        routes: {

            // execute these before any route-specific plugins
            pre: [
                // log requests
                require( 'micro-api-gateway/plugins/log' )(),

                // read a JWT called 'auth' into the request field 'tokens',
                // eg: so that you can check request.tokens.auth to verify
                // that someone has sent a proper authentication JWT for your
                // systems
                require( 'micro-api-gateway/plugins/jwt' )( {
                    name: 'auth',
                    request_field: 'tokens',
                    public_key_endpoints: config.public_key_endpoints,
                    get_from_request: request => {
                        const cookies = cookie.parse( request.headers.cookie || '' );
                        return cookies.auth;
                    },
                    public_keys: {
                        'my.issuer': 'PUBLIC KEY....' // public key to verify JWTS from your issuing domain
                    }
                } ),

                // let's allow CORS-enabled cross-origin requests for requests
                // from our domain (you should probably be a bit more selective,
                // but this is just an example)
                require( 'micro-api-gateway/plugins/cors' )( {
                    origin: 'my.domain',
                    methods: [ 'OPTIONS', 'GET', 'POST', 'PUT', 'DELETE' ]
                } )
            ],

            // execute these after any route-specific plugins
            post: [
                // let's sign the requests we make to our internal services,
                // this will add the following headers to our internal requests:
                //   x-micro-api-gateway-request-hash
                //   x-micro-api-gateway-signature
                // which we can verify are coming from this API gateway
                require( 'micro-api-gateway/plugins/sign-requests' )( {
                    key: 'PRIVATE KEY',
                    headers_to_sign: [
                        'x-some-header-we-want-to-verify-on-incoming-requests',
                        'x-some-other-header-we-care-about'
                    ]
                } )
            ]
        }
    }
} );

// let's read our APIs from any files under the 'apis' directory
const APIs = require_dir( './apis' );
Object.keys( APIs ).forEach( api => {
    const routes = APIs[ api ];
    routes.forEach( route => {
        gateway.add_route( route );
    } );
} );

// let's start listening for requests
gateway.listen( {
    port: process.env.GATEWAY_PORT || 8000
} );