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 🙏

© 2026 – Pkg Stats / Ryan Hefner

functional-router

v0.1.12

Published

Nodejs Server side and Service Worker lazy router.

Readme

Service Worker Router Build Status

Nodejs Server side and Service Worker lazy router. Difference from client, client routers can take same route on multiple locations on page. Because this router we will use in Server side and service workers. We need to handle it outside from express or any other http app.

Router taking static routes like /a/b/c, also dynamic /a/:b/:c dynamic part will return data=>data.params in same order.

  • Optional params /a(/:b) in this case will be triggered route /a and /a/b in second case b return as param.
  • Splat params supported as well a/*b when trigger route /a/some/file.js will return as param some/file.js

Router willl trigger only first match routes.

Examples

In general, router working with tasks and returning tasks.

Router subscribe to method get and return value.

import {router} from '../src/Router';
import {task} from 'functional/core/Task';

        const routes = router();
        routes.get('/a', () => {
            return 'a route'
        });

        const res = await  routes.trigger({
        //next is uri you want to trigger on router/
            next:   '/a',
            method: 'GET'
        }).unsafeRun().then(/*success promise*/);
        
        //unsafeRun() you call, because you might want to do extra actions, see complex example below.

Router subscribe to method and taking params

import {router} from '../src/Router';
import {task} from 'functional/core/Task';

        const routes = router();
        routes.get('/:a/:b', a => {
           //  a.params =['a','b'];
           
            return 'a route'
        });

        routes.trigger({
            next:   '/a/b',
            method: 'GET'
        }).unsafeRun().then(/*success promise*/);

Router subscribe, and taking nested routes.

    import {router} from 'functional-router';
    import {task} from 'functional/core/Task';

    const route = router();

    const taskB = task(b =>  b.params.concat(b.a));


    route.get('/:a', task(a => {
         const {params, next, method} = a;
         return {params, next, method}
     }).flatMap(data => {
         // trigger some nested routes useful if want to render /table or /chart with same data
         const route = router({a: data.params});
         route.get('/:b', taskB);
         return route.trigger(data);
     }));

     route.trigger({
        next:   '/a/b',
        method: 'GET'
     });
     // doing some extra general conversion  
     .map(data=>data.join(','))
     .unsafeRun()
     .then(/*success promise*/);

Full Example for Nodejs http library

Defining routes similar like another router libraries. File called rest.js


import {task} from 'functional_tasks';
import {router} from 'functional-router';

const routes = router();

routes.get('/aaa', task(({req}) => ({response: 'a route', req})));
routes.get('/aab/:a', ({req}) => req);
routes.post('/aab', task(({req}) => req || 'no Body'));

export default routes

Adding routes in app.


    import http from 'http';
    import morgan from 'morgan';
    import bodyParser from 'body-parser'
    import {task} from 'functional/core/Task';
    import {pipe, jsonHeader, response, notFound, match} from 'functional-router';
    
    import routes from './rest';
    
    const app = (req, resp) => task({req, resp})
        .through(pipe(morgan('combined')))
        .through(pipe(bodyParser.json()))
        .through(pipe(jsonHeader))
        .through(match(routes))
        .unsafeRun();
    
    http.createServer((req, resp) => {
        app(req, resp)
            .then(body => response(resp, body))
            .catch((error) => notFound(resp, error));
    
    }).listen(5050);

As you see, all plugins are compatible.

Also you can stream data.

    import {task, fileReadStream, writeStream} from 'functional_tasks';
    import {router, STREAM_END} from 'functional-router';
    
    const routes = router();
    
    const fileStream = (responseStream) => fileReadStream('./examples/streamRest/divine-comedy.txt')
        .through(writeStream(responseStream))
        .run()
        .then(() => STREAM_END);
    
    // fileStream();
    routes.get('/txt', task(({resp}) => fileStream(resp)));
    
    
    export default routes

There you hook in to stream, and returning, STREM_END flag, which notify router, stream is closed.

App will look same from above.


    import http from 'http';
    import morgan from 'morgan';
    import bodyParser from 'body-parser'
    import {task} from 'functional_tasks';
    import {pipe, htmlHeader, response, notFound, match} from 'functional-router';
    
    import routes from './stream';
    
    const app = (req, resp) => task({req, resp})
        .through(pipe(morgan('combined')))
        .through(pipe(bodyParser.json()))
        .through(pipe(htmlHeader))
        .through(match(routes))
        .unsafeRun();
    
    http.createServer((req, resp) => {
        app(req, resp)
            .then(body => response(resp, body))
            .catch((error) => notFound(resp, error));
    
    }).listen(5060);

match method support multiple routers, for example.

    import streamData from './stream';
    import rest from '../rest/rest'
    
    ...
        .through(match(streamData, rest))
    ...

See live example in examples folder.