yasinaydin/router-express


Yet another routing for Express

https://npmjs.org/package/router-express

License: MIT

Language: JavaScript


router-express Version Badge

NPM Downloads Build Status Coverage Status dependency status Code Climate Codacy Badge Join the chat at https://gitter.im/router-express/Lobby

Install

npm install --save router-express

Basic usage

var express = require('express')
var routerExpress = require('router-express')

var app = express()

var routes = [
  {
    name: 'homepage',
    url: '/',
    module: 'static/home'
  },
  {
    name: 'contact',
    url: '/contact-us',
    module: 'static/contact'
  }
]

global.Router = new RouterExpress(routes)
Router.bind(app)

Modules structure

modules/
  static/
    home/
      index.js
    contact/
      index.js

Module file

// modules/static/home/index.js
module.exports = function (req, res) {
  return res.end('Homepage')
}

URL methods

var example = Router.createUrl('contact', {foo: 'bar'}) // -> /contact-us?foo=bar
var another = Router.updateUrlWithParam(example, 'foo', undefined, res.params.route) // -> /contact-us

Advanced usage

Access routes

When you create your new RouterExpress instance, you can access all the routes via

Router.routes

List of route parameters

{
  actionFile: // @alias module
  lastOrder:
  modulesDir: // @default `path.join(__dirname, 'modules')`
  method: // @default 'get'
  module: // @alias actionFile
  name:
  order:
  params: // @default {}
  regexUrl: // @reverts to url
  regexParams:
  url:
}

Fetch route files automatically

Put your routes.json or routes.js files in every folder in your modules folder, then you can fetch all your routes and merge them with

var RouterExpress = require('router-express')

var routes = RouterExpress.fetchRoutes()
// or for your custom modules directory:
var routes = RouterExpress.fetchRoutes('myModules')

Features

  • Get a route by its name (route.name) and access its properties
  • Create a url for a route by its name (route.name) - reverse routing
  • Update a url via providing parameters
  • Inject/update parameters of a url defined in regex (regexUrl, regexParams)
  • Combine all request parameters to res.params container
  • Set default parameter values of res.params by defining them on route.params.
  • Access request's route object in action via res.params.route
  • Middleware function support before route action is called

Project Statistics

Sourcerank 5
Repository Size 84 KB
Stars 5
Forks 1
Watchers 1
Open issues 4
Dependencies 13
Contributors 2
Tags 33
Created
Last updated
Last pushed

Top Contributors See all

Ozgur Yasin Aydin The Gitter Badger

Packages Referencing this Repo

router-express
Yet another routing for Express
Latest release 1.5.2 - Updated - 5 stars

Recent Tags See all

v1.5.2 August 26, 2016
v1.5.0 August 23, 2016
v1.4.7 August 22, 2016
v1.4.6 August 22, 2016
v1.4.5 August 22, 2016
v1.4.4 August 22, 2016
v1.4.3 August 22, 2016
1.3.10 November 09, 2015
1.3.9 November 03, 2015
1.3.8 October 22, 2015
1.3.6 August 06, 2015
1.3.4 June 29, 2015
1.3.2 June 03, 2015
1.3.1 May 25, 2015
1.3.0 May 25, 2015

Something wrong with this page? Make a suggestion

Last synced: 2018-12-19 00:09:52 UTC

Login to resync this repository