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

trooba-router

v0.1.0

Published

Generic router for Trooba framework

Downloads

3

Readme

trooba-router

codecov Build Status NPM Downloads Known Vulnerabilities

The module provides a generic router for Trooba framework

It is mostly based on very fast find-my-way router and adjusted to trooba pipeline API.

The router matching is based on the pipeline context information.

  • operation (optional) defines operation, which in http case it would be set to one of http methods (GET, POST, PUT, etc.)
  • path (required) defines a specific route

The above parameters are expected to be set by a transport to pipe.context to make sure router works correctly.

Install

$ npm install trooba-router -S

Usage

Http server

const Trooba = require('trooba');
const router = require('trooba-router');

Trooba
.use('http')
.use('trooba-router', {
    'GET /path/to/resource': pipe => {
        pipe.on('request', request => {
            pipe.respond({
                status: 200,
                body: 'hello world'
            });
        })
    },
    'GET /path/to/pipe': () => {
        return Trooba.use('handler1')
            .use('handler2')
            .use(pipe => {
                pipe.respond({
                    status: 200,
                    body: 'response from other pipe'
                });
            })
            .build();
    }
})

Routing service calls

One can also use router in non-service pipelines, for example, in service invocation pipelines. This can make invocation of multiple services starting from the same point and is based on some context to match the route.

const Trooba = require('trooba');
const router = require('trooba-router');

const pipe = Trooba
.use(router, {
    'OP1 /path/to/http': pipe => {
        return Trooba
            .use('handler1')
            .use('handler2')
            .use('http', {
                hostname: 'rest.service'
            })
            .build();
    },
    'OP2 /path/to/soap': pipe => {
        return Trooba
            .use('handler1')
            .use('handler2')
            .use('soap', {
                hostname: 'soap.service'
            })
            .build();
    }
})
.build();

Routing metadata

As a pipeline grows some modules may need to know the target route before it reaches the router pipe point. For example, one would like to attach metadata to the specific route that acts as a route configuration that some middleware/handler may use during their execution.

const Trooba = require('trooba');
const router = require('trooba-router');

Trooba
.use('http')
// will attach route information to pipe.context.$route
// and handler to pipe.context.$route.handler
.use('trooba-router/match', {
    'GET /path/to/resource': {
        handler: pipe => {
            pipe.on('request', request => {
                pipe.respond({
                    status: 200,
                    body: 'hello world'
                });
            })
        },
        securityPolicy: 'authenticate',
        validateRequest: true
    },
    'GET /path/to/pipe': {
        handler: () => {
            return Trooba.use('handler1')
                .use('handler2')
                .use(pipe => {
                    pipe.respond({
                        status: 200,
                        body: 'response from other pipe'
                    });
                })
                .build();
        },
        securityPolicy: 'none'
    }
})
.use('security')
.use('validation')
// will execute route handler attached to pipe.context.$route.handler
.use('trooba-router/execute');