Lightweight route handler
- Flexible. Use as a standalone router.
- Small. Built with bundle size in mind.
- Familiar. Use express-like route syntax.
import Air from 'air-router';
// Use a hash router (default)
const router = new Air('hash');
// Use a history router (via History API)
const router = new Air('history');
router
.on('/home', () => {
console.log('home route');
})
.on('/:name', request => {
console.log(request.params.name);
});
router.start();
Register route with a
pattern
and ahandler
Start the router by listening to changes in the url.
Stop the router by unlistening to changes in the url.
Navigate to the specified
path
via thepushState
History API. If using a hash router, the#
is optional.
Retrieve an object of cookies in key-value pairs.
// Cookie: name=john
request.cookies;
// => { name: 'john' }
This property is an object containing properties mapped to the named route "parameters"
// /user/john
req.params.name;
// => 'john'
Air Router automatically parses the URI into 7 properties
Request property | Example: http://example.com:3000/pathname/?search=test#hash |
---|---|
protocol |
http: |
hostname |
example.com |
port |
3000 |
pathname |
/pathname/ |
search |
?search=test |
hash |
#hash |
host |
example.com:3000 |