https://github.com/johnsylvain/air-router
🛫 Lightweight route handler
https://github.com/johnsylvain/air-router
event-listener router typescript url-mapping url-parsing
Last synced: 28 days ago
JSON representation
🛫 Lightweight route handler
- Host: GitHub
- URL: https://github.com/johnsylvain/air-router
- Owner: johnsylvain
- License: mit
- Created: 2018-06-19T13:14:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-24T16:43:34.000Z (about 8 years ago)
- Last Synced: 2025-08-09T01:14:19.013Z (12 months ago)
- Topics: event-listener, router, typescript, url-mapping, url-parsing
- Language: TypeScript
- Homepage: https://npm.im/air-router
- Size: 76.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Air Router
> Lightweight route handler
## 👌 Features
- **Flexible.** Use as a standalone router.
- **Small.** Built with bundle size in mind.
- **Familiar.** Use express-like route syntax.
## 💻 Usage
```js
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();
```
## Methods
### `on(pattern, handler)`
> Register route with a `pattern` and a `handler`
### `start()`
> Start the router by listening to changes in the url.
### `stop()`
> Stop the router by unlistening to changes in the url.
### `go(path)`
> Navigate to the specified `path` via the `pushState` History API. If using a hash router, the `#` is optional.
## Request object
### `request.cookies`
> Retrieve an object of cookies in key-value pairs.
```js
// Cookie: name=john
request.cookies;
// => { name: 'john' }
```
### `request.params`
> This property is an object containing properties mapped to the named route "parameters"
```js
// /user/john
req.params.name;
// => 'john'
```
### URI parsed properties
> 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` |