An open API service indexing awesome lists of open source software.

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

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` |