Giter Site home page Giter Site logo

routr's Introduction

Routr

npm version Build Status Dependency Status devDependency Status Coverage Status

Routr library is an implementation of router-related functionalities that can be used for both server and client. It follows the same routing rules as Express by using the same library. This library does not use callbacks for routes, instead just mapping them to string names that can be used as application state and used within your application later. For instance in Flux, the current route would be held as state in a store.

Usage

For more detailed examples, please check out example applications;

var Router = require('routr');

var router = new Router([
    {
        name: 'view_user',
        path: '/user/:id',
        method: 'get',
        foo: {
            bar: 'baz'
        }
    },
    {
        name: 'view_user_post',
        path: '/user/:id/post/:post',
        method: 'get'
    }
]);

// match route
var route = router.getRoute('/user/garfield?foo=bar');
if (route) {
    // this will output:
    //   - "view_user" for route.name
    //   - "/user/garfield" for route.url
    //   - {id: "garfield"} for route.params
    //   - {path: "/user/:id", method: "get", foo: { bar: "baz"}} for route.config
    //   - { foo: 'bar' } for route.query
    console.log('[Route found]:', route);
}

// generate path name (does not include query string) from route
// "path" will be "/user/garfield/post/favoriteFood?meal=breakfast"
var path = router.makePath('view_user_post', {id: 'garfield', post: 'favoriteFood'}, { meal: 'breakfast' });

Object.freeze

We use Object.freeze to freeze the router and route objects for non-production environments to ensure the immutability of these objects.

For production environments, it is recommended to use tools like envify along with uglify as part of your build process to strip out the production specific code for performance benefits.

We use if (process.env.NODE_ENV !== 'production') to wrap around Object.freeze(), so that you can use various tools to build the code for different environments:

Build with Webpack

Two main utility plugins:

Example of the webpack configuration:

    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: JSON.stringify('production')
            }
        }),
        new webpack.optimize.UglifyJsPlugin(),
        ...
    ]

Build with Browserify

Similar to webpack, you can also use the following two utils with your favorite build system:

  • use envify to set process.env.NODE_ENV to the desired environment
  • use uglifyjs to remove dead code.

Command-line example:

$ browserify index.js -t [ envify --NODE_ENV production  ] | uglifyjs -c > bundle.js

API

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.

Third-pary open source code used are listed in our package.json file.

routr's People

Contributors

gigabo avatar greenkeeper[bot] avatar greenkeeperio-bot avatar irae avatar janus-toendering avatar jasisk avatar jedireza avatar koulmomo avatar lingyan avatar mridgway avatar quangv avatar redonkulus avatar sahat avatar vijar avatar webcarrot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.