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

maze-autoroute

v1.1.3

Published

Automatic routing depending on routers into a folder tree. Like on a highway you do not care about how the engine works to go to a an exit. You just turn left or right while exploring new branches of the road! This will provide you with a road'map' for su

Downloads

37

Readme

Maze-Autoroute 

Coverage Status

Mapping your routes by your routes folder structure

Getting started

  1. Create a backend folder, go into it and run these commands :

    $ npm init
       
    $ npm install maze-autoroute
    $ npm install express 
  2. Create a server.js file which has the following code :

    const express = require('express');
    const autoroute = require("maze-autoroute");
    const app = express();
    
    // ROUTES ----------------------------------------------------------
    const onmatch = ({route,module}) => app.use(route, require(module));
    autoroute.getMapping({onmatch,verbose:true});
    // END ROUTES ------------------------------------------------------
    
    // Listening parameters
    app.listen(4000, () => {
        console.log("Ready on port: " + 4000);
    });

Try it !

  1. Create the /backend/routes folder structure.
  2. Create two modules in /backend/routes : Hello and fun/World
    // ---- In backend/routes/Hello/index.js ----
    
    const express = require("express");
    const router = express.Router();
    
    router.get('/', async (req, res) => {
        res.send("Hello world! The automatic router works!");
    })
    
    router.get('/submit-sign-in', async (req, res) => {
        res.send("You are magically signed in!");
    })
    
    module.exports = router;
    
    
    // ---- In backend/routes/fun/World/index.js ----
    
    const express = require("express");
    const router = express.Router();
    
    router.get('/', async (req, res) => {
        res.send("Don't waste your time on basic routing... Just saying.");
    })
    
    router.get('/how-r-u', async (req, res) => {
        res.send("Glad you asked! I am fine!");
    })
    
    module.exports = router;

Feeling like using nodemon ?

  1. Edit the package.json file obtained to add the server script
    "scripts": {
        // ... other commands
        "server": "nodemon server.js"
     }

Customer services demo

if you do prefer having a concrete example of the autorouter without creating a brand new project, I get you covered with a small demo project on GitHub. Go check it.

Options

| Option | default | type | |--------------|---------------------------------------------------|-----------------| | onmatch | match => {} | function | | onerr | ({message}) => { console.log(message) } | function | | rootp | 'routes/' | string | | subr | null | misc | | translations | [] | array of object | | verbose | false | true |


onmatch

onmatch : on match Function to pass to be apply on every route at the final process. That process iterates over simple objects containing the final route string (route) and the path to the module (module) relative to the given base folder path given by the option rootp.

const onmatch = e => { 
    
    // destructured route item
    const { route, module } = e;
    
    express.use(route, require(module));
};

Sticking to this sniped is the better practice. Since the AutoRouter is calling onmatch anyway.


onerr

onerr : on error Function to pass to handle exceptions that can very unlikely happen during the auto routing. You may prefer to stick to the default value:

const onerr = ({message}) => { console.log(message) }; // default value

rootp

rootp : root path Defines the root folder to loop recursively to create the based route tree dynamically.

const rootp = 'routes/'; // default value

Note : the path should be relative to the server root level.


subr

subr : sub route Tells how to translate a route which is in a folder that points on a folder that is not a "leaf" in the folder tree part of rootp.

Note: Providing that special translation may avoid further eventual conflicts. Even routes work in the first place... prevention is the key!

The value of subr can be either of these :

[ null | 'b64' | 'cptlz' | { after:'<something>', before:'<something>' } ]

use like this

const subr = null ; // default value

null case : Take this following unchanged mapping. From the demo; subr is null by default:

AUTOROUTING: routes in 'routes/'
↪ [
  '/',                      #not a leaf !
  '/api',                   #not a leaf !
  '/api/customerservices',  #not a leaf !
  '/api/customerservices/cannotsee', #a leaf !
  ...,
  ]

cptlz case capitalize case : In this instance, the sub route will be capitalized ! subr is cptlz

AUTOROUTING: routes in 'routes/'
↪ [
  '/', # nothing changed for the root ... (*)

  '/Api', # /api => /Api
  '/api/Customerservices', # /api/customServices => /api/CustomServices
  '/api/customerservices/cannotsee' # a leaf !
  ...,
]

b64 case : In this following mapping ; subr is 'b64' :

AUTOROUTING: routes in 'routes/'
↪ [
  ...,
  '/api/customerservices/cannotsee',

  # it's /api/b64('customerservices') !
  '/api/Y3VzdG9tZXJzZXJ2aWNlcw==', 
  ...,
  # it's /b64('/') ! impossible to make a b64 of '' so it is b64('/')
  '/Lw==', 
  # it's /b64('api') !
  '/YXBp'
]

{before:'Hi_',after:'_Bye'} : In this following mapping ; subr is {before:'Hi_',after:'_Bye'} :

AUTOROUTING: routes in 'routes/'
↪ [
  ...,
  '/api/customerservices/cannotsee',# a leaf !
  '/api/Hi_customerservices_Bye',   # applied to /api/customerservices'
  '/Hi__Bye',                       # applied to /<empty string>
  '/Hi_api_Bye'                     # applied to /api
]

Note : you don't have to set both before and after fields for the subr because these are applied if a value is set for either before or after or both fields as checked in the AutoRouter logic

if (this.subr && this.subr.before) { /*...*/ }
if (this.subr && this.subr.after) { /*...*/ }

translations

translations Helps to customize routes in the final mapping. The Autoroute will iterate over the translations to see if a from fully matchs a route in the mapping and thus it will replace that matching route by the to string. Note : none of the from or the to should have trailing slashes '/'. For this instance, we may want to hide the infirm part of the route because it may hurt some feelings.

const translations = [{ 
        from : 'api/customerservices/special/infirm/deaf', 

        // you can issue this route like this : http://localhost:4000/deaf ... that's all !
        to : 'deaf'
    },
    {
        from : 'api/customerservices/special/infirm/blind',

        // the infirm segment has been removed! 
        // And breaking news, blind has been replaced by 'cannotsee' ! ... because blind people can't see
        to : 'api/customerservices/cannotsee'
    },
    {
        from : 'api/customerservices/special/infirm/mute',

        // Maybe desabled is more suitable than infirm but we dont want to refactor this little mistake
        to : 'api/customerservices/desabled/mute'
    }
];

verbose

verbose Tells if you want to see the final resulting route list. It has false. Turn this option to true to see a list of your auto mapped routes.

const verbose = false; // default value

Applying all options (example)

// in server.js

// CONFIGURE options
const onerr = ({message}) => { console.log(message) }; // default value
const onmatch = ({route,module}) => express.use(route, require(module)); // most important
const rootp = 'routes/'; // default value
const subr = 'b64';
const translations = [{ 
    from : 'api/customerservices/special/infirm/deaf', to : 'deaf'
}];
const verbose = false; // default value

// APPLYING the mapping of routes with all options
autoroute.getMapping({onerr,onmatch,rootp,subr,translations,verbose});

Licence

MIT