node_simple_router

0.1.4 • Public • Published

Node Simple Router

Documentation

INSTALL :

Go to your server file :

1 - require the module and give it a name : myrouter = require('node_simple_router');
2 - add this after your application setup : app.get('*', router.manageUrl);

Module configuration (config/alias.json):

- active (bool) : if set to false, you url MUST match the path to your view file.
- alias_only (bool) : if set to true, your url segments (separated by "/", must have aliases defined, direct path to file won't work by default !).
- viewFolder : the path to your view folder.
- fileExtension : you view files extension type (ex: '.jade', '.html'). YOU CAN ONLY SET ONE FOR NOW.
- page_not_found : direct path to the view file to render in case of 404 error.
- page_default : direct path to the default view if your url points to your domain name.

======================================================================================= SETTING ALIASES (make your own urls):

* aliasing are name "matches" and you will find them in config/alias.json.

Let's take an example:

Direct path to your view file (from you views main folder) = blog/mapage/index.jade
But you want your user to be redirected to that view with that URL = http://localhost:4000/mysuperblog/mapage/home,
- OR that one : http://localhost:4000/mysuperblog/mapage/mainpage

You will have to create matches (aliases) for the three folders/files guiding to your view file : 
	--> blog
		--> mapage
	 		--> index

The matches in config/alias.json would look like this for the url to works :

"matches":{
    "blog": { 
    	"matchingRoutes":["mysuperblog"]
    }, 
    "index": { 
      	"matchingRoutes":["home","mainpage"]
    },

 }

 ==> This way, http://localhost:4000/mysuperblog/mapage/home WOULD BECOME http://localhost:4000/blog/mapage/index
 IDEM for http://localhost:4000/mysuperblog/mapage/mainpage

 WHAT HAPPENED ?
   1 - "mysuperblog", if it doesn't exists as a folder, will be replaced by its match "blog". 
   2 - THEN, the module will search for "mapage", in the "blog" folder. mapage exists as a folder of the same name, so it does not need an alias !
   3 - FINALLY, "home" or "mainpage" are replaced by their match : "index"
   4 - the module render the view !

======================================================================================= OPTIONAL - SEND PARAMETERS TO YOUR VIEWS (config/routes.json) :

Imagine your want to send a parameter to the view blog/mapage/index.jade when it's called :

1 - go to config/routes.json and add this in the "routes" node :

"blog/mapage/index": { 
  "locals":{
    "title": "Hey, welcome on my blog !",
    "analytics_tracking_code": "{some string}"
  }

  2 - Those parameters will be accessible threw your view with locals.title or locals.analytics_tracking_code

Support / Contributing

You can contact me at julien.bonnin.dev@gmail.com.

Readme

Keywords

none

Package Sidebar

Install

npm i node_simple_router

Weekly Downloads

4

Version

0.1.4

License

none

Last publish

Collaborators

  • powerplex