made with  ♥  by 
React Routing

Routing and navigation solution
for JavaScript applications

20 watchers 724 stars 68 forks 0 open issues

Get Started GitHub View on GitHub
2017-10-30   |   v5.0.0 (changelog)   |   Join #universal-router chat room on Gitter to stay up to date

Universal Router

A simple middleware-style router that can be used in both client-side (e.g. React, Vue.js) and server-side applications (e.g. Node.js/Express, Koa).

Features

✓ It has simple code with only single path-to-regexp dependency
✓ It can be used with any JavaScript framework such as React, Vue.js etc
✓ It uses the same middleware approach used in Express and Koa, making it easy to learn
✓ It supports both imperative and declarative routing style
✓ Routes are plain JavaScript objects with which you can interact as you like

Installation

Using npm:

$ npm install universal-router --save

Or using a CDN like unpkg.com with the following script tag:

<script src="https://unpkg.com/universal-router@5.0.0/universal-router.min.js"></script>

How does it look like?

import UniversalRouter from 'universal-router';

const routes = [
  {
    path: '', // optional
    action: () => `<h1>Home</h1>`,
  },
  {
    path: '/posts',
    action: () => console.log('checking child routes for /posts'),
    children: [
      {
        path: '', // optional, matches both "/posts" and "/posts/"
        action: () => `<h1>Posts</h1>`,
      },
      {
        path: '/:id',
        action: (context) => `<h1>Post #${context.params.id}</h1>`,
      },
    ],
  },
];

const router = new UniversalRouter(routes);

router.resolve('/posts').then(html => {
  document.body.innerHTML = html; // renders: <h1>Posts</h1>
});

Play with an example on JSFiddle, CodePen, JS Bin in your browser or try RunKit node.js playground.

Documentation

Browser Support

Universal Router supports all popular browsers, including Internet Explorer 9 and above with polyfills such as es6-shim for Map, Promise and Object.assign that must be included before any other code.

For compatibility with older browsers you may also need to include polyfills for Array.isArray and Object.create.

Books and Tutorials

Sponsors

Does your company use Universal Router in production? Please consider sponsoring this project. Your help will allow maintainers to dedicate more time and resources to its development and support.

Backers

♥ Universal Router? Become a backer to show your support and help us maintain and improve this open source project. Get your image on our README with a link to your site.

Support

Contributing

Anyone and everyone is welcome to contribute to this project. The best way to start is by checking our open issues, submit a new issues or feature request, participate in discussions, upvote or downvote the issues you like or dislike, send pull requests.