1.0.5 • Published 8 years ago

@synapse/minroute2 v1.0.5

Weekly downloads
1
License
MIT
Repository
-
Last release
8 years ago

This is a component of the overall project called minproject, a suite of minimalist web components designed to get web projects up and running fast without ugly callback code and bloated middlewarez. Relies heavily on the node.js API, this is the heart of minproject.

What is it? - minroute2 is a minimalist router initialiser. It will build routing folders, fouting configuration and project configuration files. minroute2 exposes 2 functions: initRoutes() and getRoutes().

minroute does not DEPEND on mininit but it is designed to be used with mininit.

1) mininit generates a project globals object, config dir and config file in the format of:

setup = {
{ project_settings:
    [ { osType: 'win32',
        pathType: '\\',
        rootDir: 'C:\\Users\\jsmur\\Documents\\Programs\\NodeJS\\NODE.APPS\\minproject',
        configDir: 'config',
        configFile: 'project-settings.json' } ],
 db_settings: [ { host: 'local', port: 200, db: 'test' } ] }

2) minroute2 will accept the above object into the constructor function. It will then check for an entry under the key 'project_settings' called 'routesFile', if no entry is found the entry will be added to the passed object in memory and then it will be saved to disk. It's this that enables minroute to work without mininit, no dependencies.

minroute2 will use the rootDir property, configDir and configFile properties in the passed object to save the file to disk, so it is advisable to ensure the directory specified in configDir exists before passing this to minroute though in my testing configDir and configFile do not need to exist, they will be written but it depends on your OS permissions.

3) If minroute is used with mininit you don't need to do anything, your project will be initialised automatically. USAGE

Usage is like the following:

var mininit = require('@synapse/mininit'); var minroute = require('@synapse/minroute');

mininit.initConfig({host:'localhost', port:27017, db:'dbname'});

minroute.initRoutes(mininit.getConfig());

console.log(minroute.getRoutes());

3 a) Edit your routes.json file to the resources you want to serve and copy your assets (html, css) to the folders built for you and that's all you need to do for a working web server.

3 b) Want a new template? simply delete the project_settings.json from config directory, navigate to router.js under minroute/lib, edit the SERVE_ROOT_DIR_NAME variable to something like 'myTemplateTwo' and re run your project.

4) minroute.getRoutes() will return an object that holds the routing information as it's own property (not a prototype) and the config dir will have a routes.json. Additionally, mininit.getConfig() returns the config object which will have a new property called 'requested_paths' generated by minroute which will have path names for all public folders to be used for the router.

5) All folders and filenames produced by mininit and minroute can be edited safely if you so desire, as they are specified as constants at the top of their core lib files.

6) Example usage of minroute without mininit:

var minroute = require('@synapse/minroute');

var setup = {};

setup = {

project_settings: { osType: 'win32', pathType: '\', rootDir: 'C:\Users\jsmur\Documents\Programs\NodeJS\NODE.APPS\testModule', configDir: 'config', configFile: 'project-settings.json' } , db_settings: { host: 'local', port: 200, db: 'test' } };

minroute.initRoutes(setup);

console.log(minroute.getRoutes());