1 | <a href="https://www.kriasoft.com/universal-router/" target="_blank">
|
2 | <img width="64" height="64" align="right" alt="Visit Universal Router Website"
|
3 | src="https://raw.githubusercontent.com/kriasoft/universal-router/master/docs/assets/img/tour.png" />
|
4 | </a>
|
5 |
|
6 | # Universal Router
|
7 |
|
8 | [![NPM version](http://img.shields.io/npm/v/universal-router.svg?style=flat-square)](https://www.npmjs.com/package/universal-router)
|
9 | [![NPM downloads](http://img.shields.io/npm/dm/universal-router.svg?style=flat-square)](https://www.npmjs.com/package/universal-router)
|
10 | [![Build Status](http://img.shields.io/travis/kriasoft/universal-router/master.svg?style=flat-square)](https://travis-ci.org/kriasoft/universal-router)
|
11 | [![Coverage Status](https://img.shields.io/coveralls/kriasoft/universal-router.svg?style=flat-square)](https://coveralls.io/github/kriasoft/universal-router)
|
12 | [![Dependency Status](http://img.shields.io/david/kriasoft/universal-router.svg?style=flat-square)](https://david-dm.org/kriasoft/universal-router)
|
13 | [![Online Chat](http://img.shields.io/badge/chat_room-%23universal--router-blue.svg?style=flat-square)](https://gitter.im/kriasoft/universal-router)
|
14 |
|
15 | A simple middleware-style router that can be used in both client-side (e.g. React, Vue.js)
|
16 | and server-side applications (e.g. Node.js/Express, Koa).
|
17 |
|
18 | Visit **[Quickstart Guide](http://slides.com/koistya/universal-router)** (slides) |
|
19 | Join **[#universal-router](https://gitter.im/kriasoft/universal-router)** on Gitter to stay up to date
|
20 |
|
21 |
|
22 | ## Features
|
23 |
|
24 | ✓ It has [simple code](https://github.com/kriasoft/universal-router/blob/master/src/Router.js)
|
25 | with only single [path-to-regexp](https://github.com/pillarjs/path-to-regexp) dependency<br>
|
26 | ✓ It can be used with any JavaScript framework such as React, Vue.js etc<br>
|
27 | ✓ It uses the same middleware approach used in Express and Koa, making it easy to learn<br>
|
28 | ✓ It supports both [imperative](https://en.wikipedia.org/wiki/Imperative_programming) and
|
29 | [declarative](https://en.wikipedia.org/wiki/Declarative_programming) routing style<br>
|
30 | ✓ Routes are plain JavaScript objects with which you can interact as you like<br>
|
31 |
|
32 |
|
33 | ## Installation
|
34 |
|
35 | Using [npm](https://www.npmjs.com/package/universal-router):
|
36 |
|
37 | ```bash
|
38 | $ npm install universal-router --save
|
39 | ```
|
40 |
|
41 | Or using a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network) like [unpkg.com](https://unpkg.com)
|
42 | with the following script tag:
|
43 |
|
44 | ```html
|
45 | <script src="https://unpkg.com/universal-router@3.2.0/universal-router.min.js"></script>
|
46 | ```
|
47 |
|
48 |
|
49 | ## How does it look like?
|
50 |
|
51 | ```js
|
52 | import UniversalRouter from 'universal-router';
|
53 |
|
54 | const routes = [
|
55 | {
|
56 | path: '/',
|
57 | action: () => `<h1>Home</h1>`
|
58 | },
|
59 | {
|
60 | path: '/posts',
|
61 | action: () => console.log('checking child routes for /posts'),
|
62 | children: [
|
63 | {
|
64 | path: '/',
|
65 | action: () => `<h1>Posts</h1>`
|
66 | },
|
67 | {
|
68 | path: '/:id',
|
69 | action: (context) => `<h1>Post #${context.params.id}</h1>`
|
70 | }
|
71 | ]
|
72 | },
|
73 | ];
|
74 |
|
75 | const router = new UniversalRouter(routes);
|
76 |
|
77 | router.resolve('/posts').then(html => {
|
78 | document.body.innerHTML = html; // renders: <h1>Posts</h1>
|
79 | });
|
80 | ```
|
81 |
|
82 | Play with an example on [JSFiddle](https://jsfiddle.net/frenzzy/b0w9mjck/),
|
83 | [CodePen](https://codepen.io/frenzzy/pen/aWLKpb/),
|
84 | [JS Bin](https://jsbin.com/degedol/edit?js,output) in your browser or try
|
85 | [RunKit](https://runkit.com/frenzzy/universal-router-demo) node.js playground.
|
86 |
|
87 |
|
88 | ## Documentation
|
89 |
|
90 | * [Overview](https://github.com/kriasoft/universal-router/blob/master/docs/README.md)
|
91 | * [Getting Started](https://github.com/kriasoft/universal-router/blob/master/docs/getting-started.md)
|
92 | * [Universal Router API](https://github.com/kriasoft/universal-router/blob/master/docs/api.md)
|
93 |
|
94 |
|
95 | ## Browser Support
|
96 |
|
97 | Universal Router supports all popular browsers, including Internet Explorer 9 and above
|
98 | with polyfills such as [es6-shim](https://github.com/es-shims/es6-shim) for
|
99 | [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),
|
100 | [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and
|
101 | [`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
|
102 | that must be included before any other code.
|
103 |
|
104 | For compatibility with older browsers you may also need to include polyfills for
|
105 | [`Array.isArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
|
106 | and [`Object.create`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create).
|
107 |
|
108 |
|
109 | ## Books and Tutorials
|
110 |
|
111 | :mortar_board: **[ES6 Training Course](https://es6.io/friend/konstantin)** by Wes Bos<br>
|
112 | :green_book: **[You Don't Know JS: ES6 & Beyond](http://amzn.to/2bFss85)** by Kyle Simpson (Dec, 2015)<br>
|
113 |
|
114 |
|
115 | ## Sponsors
|
116 |
|
117 | Does your company use Universal Router in production? Please consider
|
118 | [sponsoring this project](https://opencollective.com/universal-router#sponsor).
|
119 | Your help will allow maintainers to dedicate more time and resources to its development and support.
|
120 |
|
121 | <a href="https://opencollective.com/universal-router/sponsor/0/website" target="_blank">
|
122 | <img src="https://opencollective.com/universal-router/sponsor/0/avatar.svg" height="64">
|
123 | </a>
|
124 | <a href="https://opencollective.com/universal-router/sponsor/1/website" target="_blank">
|
125 | <img src="https://opencollective.com/universal-router/sponsor/1/avatar.svg">
|
126 | </a>
|
127 | <a href="https://opencollective.com/universal-router/sponsor/2/website" target="_blank">
|
128 | <img src="https://opencollective.com/universal-router/sponsor/2/avatar.svg">
|
129 | </a>
|
130 | <a href="https://opencollective.com/universal-router/sponsor/3/website" target="_blank">
|
131 | <img src="https://opencollective.com/universal-router/sponsor/3/avatar.svg">
|
132 | </a>
|
133 | <a href="https://opencollective.com/universal-router/sponsor/4/website" target="_blank">
|
134 | <img src="https://opencollective.com/universal-router/sponsor/4/avatar.svg">
|
135 | </a>
|
136 | <a href="https://opencollective.com/universal-router/sponsor/5/website" target="_blank">
|
137 | <img src="https://opencollective.com/universal-router/sponsor/5/avatar.svg">
|
138 | </a>
|
139 | <a href="https://opencollective.com/universal-router/sponsor/6/website" target="_blank">
|
140 | <img src="https://opencollective.com/universal-router/sponsor/6/avatar.svg">
|
141 | </a>
|
142 | <a href="https://opencollective.com/universal-router/sponsor/7/website" target="_blank">
|
143 | <img src="https://opencollective.com/universal-router/sponsor/7/avatar.svg">
|
144 | </a>
|
145 | <a href="https://opencollective.com/universal-router/sponsor/8/website" target="_blank">
|
146 | <img src="https://opencollective.com/universal-router/sponsor/8/avatar.svg">
|
147 | </a>
|
148 | <a href="https://opencollective.com/universal-router/sponsor/9/website" target="_blank">
|
149 | <img src="https://opencollective.com/universal-router/sponsor/9/avatar.svg">
|
150 | </a>
|
151 |
|
152 |
|
153 | ## Backers
|
154 |
|
155 | ♥ Universal Router? [Become a backer](https://opencollective.com/universal-router#backer)
|
156 | to show your support and help us maintain and improve this open source project.
|
157 | Get your image on our README with a link to your site.
|
158 |
|
159 | <a href="https://opencollective.com/universal-router/backer/0/website" target="_blank">
|
160 | <img src="https://opencollective.com/universal-router/backer/0/avatar.svg" height="64">
|
161 | </a>
|
162 | <a href="https://opencollective.com/universal-router/backer/1/website" target="_blank">
|
163 | <img src="https://opencollective.com/universal-router/backer/1/avatar.svg">
|
164 | </a>
|
165 | <a href="https://opencollective.com/universal-router/backer/2/website" target="_blank">
|
166 | <img src="https://opencollective.com/universal-router/backer/2/avatar.svg">
|
167 | </a>
|
168 | <a href="https://opencollective.com/universal-router/backer/3/website" target="_blank">
|
169 | <img src="https://opencollective.com/universal-router/backer/3/avatar.svg">
|
170 | </a>
|
171 | <a href="https://opencollective.com/universal-router/backer/4/website" target="_blank">
|
172 | <img src="https://opencollective.com/universal-router/backer/4/avatar.svg">
|
173 | </a>
|
174 | <a href="https://opencollective.com/universal-router/backer/5/website" target="_blank">
|
175 | <img src="https://opencollective.com/universal-router/backer/5/avatar.svg">
|
176 | </a>
|
177 | <a href="https://opencollective.com/universal-router/backer/6/website" target="_blank">
|
178 | <img src="https://opencollective.com/universal-router/backer/6/avatar.svg">
|
179 | </a>
|
180 | <a href="https://opencollective.com/universal-router/backer/7/website" target="_blank">
|
181 | <img src="https://opencollective.com/universal-router/backer/7/avatar.svg">
|
182 | </a>
|
183 | <a href="https://opencollective.com/universal-router/backer/8/website" target="_blank">
|
184 | <img src="https://opencollective.com/universal-router/backer/8/avatar.svg">
|
185 | </a>
|
186 | <a href="https://opencollective.com/universal-router/backer/9/website" target="_blank">
|
187 | <img src="https://opencollective.com/universal-router/backer/9/avatar.svg">
|
188 | </a>
|
189 |
|
190 |
|
191 | ## Related Projects
|
192 |
|
193 | * [React Starter Kit](https://github.com/kriasoft/react-starter-kit) —
|
194 | Isomorphic web app boilerplate (Node.js, React, GraphQL, Webpack, CSS Modules)
|
195 | * [Node.js API Starter Kit](https://github.com/kriasoft/nodejs-api-starter) —
|
196 | Boilerplate and tooling for building data APIs with Node.js, GraphQL and Relay
|
197 | * [ASP.NET Core Starter Kit](https://github.com/kriasoft/aspnet-starter-kit) —
|
198 | Cross-platform single-page application boilerplate (ASP.NET Core, React, Redux)
|
199 | * [Babel Starter Kit](https://github.com/kriasoft/babel-starter-kit) —
|
200 | JavaScript library boilerplate (ES2015, Babel, Rollup, Mocha, Chai, Sinon, Rewire)
|
201 | * [React App SDK](https://github.com/kriasoft/react-app) —
|
202 | Create React apps with just a single dev dependency and zero configuration
|
203 | * [React Static Boilerplate](https://github.com/koistya/react-static-boilerplate) —
|
204 | Single-page application (SPA) starter kit (React, Redux, Webpack, Firebase)
|
205 | * [History](https://github.com/mjackson/history) —
|
206 | HTML5 History API wrapper library that handle navigation in single-page apps
|
207 |
|
208 |
|
209 | ## Support
|
210 |
|
211 | * [#universal-router](https://gitter.im/kriasoft/universal-router) on Gitter —
|
212 | Watch announcements, share ideas and feedback
|
213 | * [GitHub Issues](https://github.com/kriasoft/universal-router/issues) —
|
214 | Check open issues, send feature requests
|
215 | * [@koistya](https://twitter.com/koistya) on [Codementor](https://www.codementor.io/koistya),
|
216 | [HackHands](https://hackhands.com/koistya/)
|
217 | or [Skype](https://hatscripts.com/addskype?koistya) — Private consulting
|
218 |
|
219 |
|
220 | ## Contributing
|
221 |
|
222 | Anyone and everyone is welcome to
|
223 | [contribute](https://github.com/kriasoft/universal-router/blob/master/CONTRIBUTING.md) to this project.
|
224 | The best way to start is by checking our [open issues](https://github.com/kriasoft/universal-router/issues),
|
225 | [submit a new issues](https://github.com/kriasoft/universal-router/issues/new?labels=bug) or
|
226 | [feature request](https://github.com/kriasoft/universal-router/issues/new?labels=enhancement),
|
227 | participate in discussions, upvote or downvote the issues you like or dislike, send [pull
|
228 | requests](https://github.com/kriasoft/universal-router/blob/master/CONTRIBUTING.md#pull-requests).
|
229 |
|
230 |
|
231 | ## License
|
232 |
|
233 | Copyright © 2015-present Kriasoft, LLC.
|
234 | This source code is licensed under the MIT license found in the
|
235 | [LICENSE.txt](https://github.com/kriasoft/universal-router/blob/master/LICENSE.txt) file.
|
236 | The documentation to the project is licensed under the
|
237 | [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/) license.
|
238 |
|
239 |
|
240 | ---
|
241 | Made with ♥ by
|
242 | Konstantin Tarkus ([@koistya](https://twitter.com/koistya), [blog](https://medium.com/@tarkus)),
|
243 | Vladimir Kutepov ([frenzzy](https://github.com/frenzzy)) and
|
244 | [contributors](https://github.com/kriasoft/universal-router/graphs/contributors)
|