rfm

route-file-mapper

A dynamic route file organization system and auto-loader for Express.

Showing:

Popularity

Downloads/wk

0

Maintenance

No Maintenance Data Available

Package

Dependencies

1

Size (min+gzip)

3.9KB

License

Type Definitions

Tree-Shakeable

No?

Categories

Deprecated!
'owner's

Readme

route-file-mapper

A dynamic route file-organization system and auto-loader for Express with support for logging to console or to a Winston logger.

Installation

$ npm install route-file-mapper

Purpose

The purpose of the route-file-mapper module is to provide a more streamline way to add basic routes to your node/express app in an intuitive way. It also prints out a map which makes it more easy to see where each route is located as well as what type of HTTP request is made with each one.

Usage

Rather than calling var routes = require(routes.js) and then app.get('/path', route.insertMethodHere) as we typically do in a node/express app, a sub-directory of routes can be provided which each provide their own information regarding their path, method (of HTTP request made) and handler which takes care of the apps functionality (given the same arguments as you would to a typical express app instance; req, res, and an optional next). The path specified searches through each possible outcome and discovers .js files that are formatted to be routes. It will do this recursively through each sub-route from the path given.

So, as an arbitrary example, here would be two route files to be used by this our file mapper (along with another thrown in for demonstration purposes):

routes/login.js

module.exports = 
{
    path: '/login',
    method: 'post',
    handler : function(req, res)
    {
        if(req.body.user == 'rob' && req.body.pw=='awesomesauce')
        {
            //insert more code here
            res.send('Not a very good example but you\'re logged in.'); 
        }
    }
};

routes/home.js

module.exports = 
{
    path: '/',
    method: 'get',
    handler : function(req, res)
    {
        res.render('index.html', {title: 'Hello World. G\'day sir.'});
    }
};

routes/extra/some-random-js.js

console.log('meh.');
.
 app.js
 routes
     home.js
     login.js
     extra/
         some-random-js.js 

Upon calling:

var rfMapper = require('route-file-mapper');
rfMapper.scanForRoutes({ path: __dirnam + '/routes', caller: app });

Our server connected to the app specified by the caller argument will now listen on / with a get request as well as /login on a post request.

As routes/extra/some-random-js.js is a blob of JavaScript that does not have the required path, method, and handler defined within an exported module, we will not have it appended to the app or logged while scanning through the files.

##Logging Route Map (either via Winston or Console) To support logging, when calling rfMapper.scanForRoutes(), simply set a few options in the params.options argument; mainly: disableLogging must be set to false. If no instance argument is supplied to the logger parameter, then we will default to using the console. Otherwise, we can set the type to 'winston', which will use an instance of the winston logger.

Note: in the future, this will be allowed to be set up with an init method.

logging-example.js

var rfMapper = require('route-file-mapper');
var winston  = require('winston');  //use a default winston logger instance

rfMapper.scanForRoutes(
{
    caller : app,                   //reference to express app instance
    path   : __dirname + '/routes'  //where to scan for routes
    options: 
    {
        disableLogging : false      //log map output,
        logger :                    
        {
            type    : 'winston',    //only winston supported so far
            instance:  winston      //supply our logging obj
            level   : 'info'        //level of logging to write
        }
    }
});

##API

var rfMapper = require('route-file-mapper');

###rfMapper.scanForRoutes(params) Scans a specific path recursively for different route .js files, then calls them in reference from an Express application or, in Express 4.0 or higher, a Router instance.

####params.path {String | Array} defines the absolute path where to start searching routes. This can be an array of aliases for a route, or a single string to refer the route to.

####params.caller {Object} instance of an express app or a Router that originally contains the http methods to listen in on.

####params.callback {Function} action to take when all routes have been added (optional)

####params.options {JSONObject} set misc configuration properties such as whether to print text or colors

The available options are:

  • disableLogging - allows for disabling log of route details during scan (default: false)
  • logger - allows to use a winston (and possibly in the future other) logger to output with. See above for an example of using a logger.
  • endMarker - a marker which is displayed at the end of processing every sub-directory, also a connotation for the level of depth. Set this to '' (blank string) to disregard markers and proceeding newlines completely (default: '*').
  • colors - defines the colors to print to console (using the colors module). Configuration for that is:
    • routePaths color of the route paths (default: gray)
    • endMarkers color of the directory scan end markers (default: green)
    • arrows color of the newline -> arrows (default: gray)
    • methods color of the methods(POST/GET/PUT/etc) (default: blue)

License

Copyright (c) 2014-2015 Robert Concepcion III

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100