Socket
Socket
Sign inDemoInstall

trouter

Package Overview
Dependencies
2
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    trouter

🐟 A fast, small-but-mighty, familiar ~fish~ router


Version published
Maintainers
1
Install size
20.1 kB
Created

Readme

Source

trouter Build Status

🐟 A fast, small-but-mighty, familiar fish router

Install

$ npm install --save trouter

Usage

const Trouter = require('trouter');
const router = new Trouter();

// Define all routes
router
  .get('/users', _ => {
    console.log('> Getting all users');
  })
  .add('POST', '/users', _ => {
    console.log('~> Adding a user');
  })
  .get('/users/:id', val => {
    console.log('~> Getting user with ID:', val);
  });

// Find a route definition
let obj = router.find('GET', '/users/123');
//=> obj.params ~> { id:123 }
//=> obj.handler ~> Function

// Execute the handler, pass value
obj.handler( obj.params.id );
//=> ~> Getting user with ID: 123

// Returns `false` if no match
router.find('DELETE', '/foo');
//=> false

API

Trouter()

Initializes a new Trouter instance. Currently accepts no options.

trouter.add(method, pattern, handler)

Returns: self

Stores a method + pattern pairing internally, along with its handler.

method

Type: String

Any valid HTTP method name.

pattern

Type: String

Unlike most router libraries, Trouter does not use RegExp to determine pathname matches. Instead, it uses string comparison which is much faster, but also limits the pattern complexity.

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 function that should be tied to this pattern.

Important: Trouter does not care what your function signature looks like!
You are not bound to the (req, res) standard.

trouter.all(pattern, handler)

Returns: self

This is an alias for trouter.add('*', pattern, handler), matching all HTTP methods.

Important: If the pattern used within all() exists for a specific method as well, then only the method-specific entry will be returned!

router.post('/hello', () => 'FROM POST');
router.add('GET', '/hello', () => 'FROM GET');
router.all('/hello', () => 'FROM ALL');

router.find('GET', '/hello').handler();
//=> 'FROM GET'
router.find('POST', '/hello').handler();
//=> 'FROM POST'
router.find('DELETE', '/hello').handler();
//=> 'FROM ALL'
router.find('PUT', '/hello').handler();
//=> 'FROM ALL'

trouter.METHOD(pattern, handler)

This is an alias for trouter.add(METHOD, pattern, handler), where METHOD is any lowercased HTTP method name.

const noop = _ => {}:
const app = new Trouter();

app.get('/users/:id', noop);
app.post('/users', noop);
app.patch('/users/:id', noop);

// less common methods too
app.trace('/foo', noop);
app.purge('/bar', noop);
app.copy('/baz', noop);

trouter.find(method, url)

Returns: Object|Boolean
Searches within current instance for a method + pattern pairing that matches the current method + url.

This method will return false if no match is found. Otherwise it returns an Object with params and handler keys.

method

Type: String

Any valid HTTP method name.

url

Type: String

The URL used to match against pattern definitions. This is typically req.url.

Benchmarks

Run on Node v8.9.0

GET / ON /
  --> 9,548,621 ops/sec ±0.65% (96 runs sampled)

POST /users ON /users
  --> 2,324,166 ops/sec ±0.52% (93 runs sampled)

GET /users/123 ON /users/:id
  --> 1,704,811 ops/sec ±0.50% (95 runs sampled)

PUT /users/123/books ON /users/:id/books/:title?
  --> 1,396,875 ops/sec ±0.14% (94 runs sampled)

DELETE /users/123/books/foo ON /users/:id/books/:title
  --> 1,266,708 ops/sec ±0.59% (95 runs sampled)

HEAD /hello on /hello -- via all()
  --> 1,641,558 ops/sec ±0.14% (96 runs sampled)

License

MIT © Luke Edwards

Keywords

FAQs

Last updated on 02 May 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc