0.0.3 • Published 6 years ago

air-router v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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

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.

// Cookie: name=john
request.cookies;
// => { name: 'john' }

request.params

This property is an object containing properties mapped to the named route "parameters"

// /user/john
req.params.name;
// => 'john'

URI parsed properties

Air Router automatically parses the URI into 7 properties

Request propertyExample: http://example.com:3000/pathname/?search=test#hash
protocolhttp:
hostnameexample.com
port3000
pathname/pathname/
search?search=test
hash#hash
hostexample.com:3000
0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago