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

basic-router

v0.0.1

Published

Basic router with filters built in, part of basicJS framework

Downloads

7

Readme

basicRouter

Javascript routing in a basic way

Installation

$ npm install basic-router

Typical use

When a route is matched we use the callback, so first, we declare our callbacks, in the example I declared _default and _user. Then, we need to tell the Router our routes using Router.addRoutes and passing an object in which we declare our routes, which controller we use in case of match and which one are params and how to match them.

var _default = function() {
   console.log('default');
   Router.change('user', '/user/123/some/thing');
};

var _user = function(id, thing) {
   console.log(id, thing);
};

Router.addRoutes({
   '/user/id/some/thing': {
      controller: _user,
      params: {
         id: '\\d{1,3}',
         thing: '[a-z]{1,6}'
      }
   },
   'default': {
      controller: _default
   }
});

Router.run();

If we run our app, with no pathname like example.com, our router will call to 'default' callback, so, its mandatory for you to declare a callback for 'default'. So our routes object would be like this:

{
   'default': {
      controller: _default
   }
}

Then we can add our own routes with some magic:

var routes = {
   '/user/id/some/thing': {
      controller: _user,
      params: {
         id: '\\d{1,3}',
         thing: '[a-z]{1,6}'
      }
   },
   'default': {
      controller: _default
   }
}

In the previous example we declare '/user/id/some/thing' but pay attention to the params, we tell the router that id and thing are wildcards and those will match with certain regex (don't be afraid, here you need to use short and easy ones) id will match with all numbers between 0 and 999, meanwhile thing will match with chars from a to z with a max of 6 characters.

Now you can filter in the way you want using commons regex and without declare url with extra symbols. If the url cannot match the router will use default callback, so, we encourage to use default like home. Its very weird the case developers use another url for default so, I decide to work a bit less on that.

Also you can add more routes, and will merge new ones with old ones, delete routes if no longer needed, flush all routes of the registry. And of course, change the URL.

A few more routes:

Router.addRoute({
    '/login/user': {
        controller: _login,
        params: {
            user: '\\d{1,8}'
        } 
    },
    '/login/email': {
            controller: _loginWithEmail,
            params: {
                email: 'some email regex'
            } 
        }
});

We can see the potential of this router, we can modularize our app entirely, every module can manage his own routes, and we can achieve a kind of strategy pattern with no difficulties.

If we decide the user only can login with email we can delete

Router.delRoute('/login/user');

If we get mad, we can flush all, but this would save the default call.

Router.flushRoutes();

If we want to navigate to another url just use Route.change

Router.change('user', '/user/[email protected]');

Enjoy it!