8000 GitHub - herber/shipharbor: ๐Ÿšข๐ŸŒŠ A tiny router, that runs in browsers and in nodejs
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

herber/shipharbor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Shipharbor

๐Ÿšข๐ŸŒŠ Shipharbor is a tiny router, that runs in browsers and in nodejs.

Build Status

Install

$ npm install shipharbor

Usage

const Shipharbor = require('shipharbor');
const router = new Shipharbor();

router.add('/', () => {
  console.log('> Home');
});

router.add('/user/:id', (params) => {
  console.log('> User: ' + params.id);
});

router.add('/*', () => {
  console.log('> Not found');
});

router.match('/').handler();
// > Home

const match = router.match('/user/my-user');
match.handler(match.params);
// > User: my-user

router.match('/whatever').handler();
// > Not found

API

shipharbor()

add(path, handler)

path

Type: string

A string representing the path you want to match. This is similar to express's paths.

The supported pattern types are:

  • static (/users)
  • named parameters (/users/:id)
  • nested parameters (/users/:id/books/:title)
  • optional parameters (/users/:id?/books/:title?)
  • any match / wildcards (/users/*)
handler

Type: function

The handler function for a specific path.

match(path)

Returns: object|boolean;

path

Type: string

The url you want to match against the previously specified routes.

Example:
const match = router.match('/route');
// if the route exists:
//  => { handler, params }
// else
//  => false

match.handler(); // executes the handler

License

MIT ยฉ Tobias Herber

About

๐Ÿšข๐ŸŒŠ A tiny router, that runs in browsers and in nodejs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0