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

s-router

v0.9.9

Published

The router for cutting middleware by endpoints

Downloads

1

Readme

NPM version License

s-router

Router for slicing a middleware by endpoints.

installation

npm i s-router --save

Router map

The router offers several abstractions to build a route-map. Using these abstractions to create a clear and easy to use / configuring, a route map. It does not impose restrictions on the formation of your server architecture. And it allows you to distribute of the configuration and the execution of the routing so as you wish.

queue - the sequence in which the handler will be executed

Params - instances with results of handlers. Which make transmitting the data between them.

router - owner of map with handlers

endpoint - one of the routes of the router

unit - route element


Example configuring

To understand the the map makes sense to describe the endpoints in one place. This is not the only option, but popular.

var mapper = require('s-router');
// create instance of router
var router = mapper('id-of-this-router'); 
// create instance of endpoint to handling /some/api/test/anyString[/] or /some/api/test[/]
var endpoint = router.endpoint('test-id-of-endpoint', '/some/api/{?:test}');
// create instance of endpoint to handling /some/api/test2/anyString[/]
var endpoint2 = router.endpoint('test-id-of-endpoint2', '/some/api/{:test2}');

Example handling

When we already have started endpoints. We can refer to them by name. And set queue of handlers which would create response. Each method of adding processors can take as a function or an array of functions.


var router = require('s-router')('id-of-this-router')

router
    // '/some/p1/1231'
    .endpoint('test-id-of-endpoint', 'some/{:p1}')
    .get(function ( request, response, params ) {
        params.prepareSome = 2;
    })
    .get(function ( request, response, params ) {
        params.prepareSome // 2
        response.end('nothing');
    })
    .post(function ( request, response, params ) {
        response.end('nothing');
    })
    .error(function ( request, response, params ) {
        params.error // contain error message for this endpoint
        response.end('error');
    })
    .use(function ( request, response, params ) {
        // execute before queue of endpoint

    })
    .use(function ( request, response, params ) {
        // execute before queue of endpoint but after previous endpoint 'use' handler
        // all next in the queue be wait this promise
        return new Promise(function ( resolve, reject ) { resolve(); });
    });

Example pretreatment

In turn, the instance router may pretreated with any request. Each request and by methods. It contains the same methods as the endpoint. But his handlers are added to the top of the queue, before the queue of end point.


var router = require('s-router')('id-of-this-router')

router.get(function ( request, response, params ) {
        // execute before get queue each of endpoints
    })
    .post(function ( request, response, params ) {
        // execute before post queue each of endpoints
    })
    .error(function ( request, response, params ) {
        // execute after errors each of endpoints
    })
    .use(function ( request, response, params ) {
        // execute before queue each of endpoints
    })
    .use(function ( request, response, params ) {
        // execute before queue each of endpoint but after previous router 'use' handler
        // all next in the queue be wait this promise
        return new Promise(function ( resolve, reject ) { resolve(); });
    });

Example Params extend

An instance of the parameters(third arguments) for each request is new. If it becomes necessary to add the values - instance of router involves functionality the expansion prototype of the constructor Params. How it looks.


var router = require('s-router')('id-of-this-router')

router.extendParams(function () {
    this.test = function () {
        console.log('I am a method - which will be available in all handlers like a params.test()');
    };
});

Example Unit

Endpoints may use the same preparation handlers. They can put into groups called by actions they solve.


var router = require('s-router')('id-of-this-router')

router.unit('someUnit', '/some/{:api}')
    .get(function ( request, response, params ) {
        params.options.api;
        params.somePrepare.push(1);
        console.log('unit some prepering action on GET');
    })
    .use(function ( request, response, params ) {
        params.somePrepare = [];
        console.log('unit some prepering action on each request');
    });
    
// create endpoint wich handle '/some/api/1/p1/1231'
router.endpoint('someUnit.id-of-endpoint', '/{:p1}')
    .get(function ( request, response, params ) {
        params.options.p1; // => 1231
        params.options.api; // => 1
        params.somePrepare; // => [1]
    });
    
// create endpoint wich handle '/some/api/2/p2/1231/id/2'
router.endpoint('someUnit.id-enother', '/{:p2}/{?:id}')
    .get(function ( request, response, params ) {
        params.options.p1; // => undefined
        params.options.p2; // => 1231
        params.options.id; // => 2
        params.options.api; // => 2
        params.somePrepare; // => [1]
    })

Note: You can build your map as you wish. These are only examples, "as it could be"