summaryrefslogtreecommitdiff
path: root/alarm/node_modules/trouter/readme.md
blob: 0f25d3f0c655608f29b5ea2ed460b18befa8c33b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# trouter [![Build Status](https://travis-ci.org/lukeed/trouter.svg?branch=master)](https://travis-ci.org/lukeed/trouter)

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


## Install

```
$ npm install --save trouter
```


## Usage

```js
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.handlers ~> Array<Function>

// Execute the handlers, passing value
obj.handlers.forEach(fn => {
  fn(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, ...handlers)
Returns: `self`

Stores a `method` + `pattern` pairing internally, along with its handler(s).

#### method
Type: `String`

Any lowercased, [valid HTTP/1.1 verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods#Specifications) &mdash; choose from one of the following:

```
GET  HEAD  PATCH  OPTIONS  CONNECT  DELETE  TRACE  POST  PUT
```

#### 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/*`)

#### handlers
Type: `Array|Function`

The function(s) that should be tied to this `pattern`.

Because this is a [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), whatever you pass will _always_ be cast to an Array.

> **Important:** Trouter does not care what your function signature looks like!<br> You are not bound to the `(req, res)` standard, or even passing a `Function` at all!

### trouter.all(pattern, ...handlers)
Returns: `self`

This is an alias for [`trouter.add('*', pattern, ...handlers)`](#trouteraddmethod-pattern-handlers), 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!

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

let { handlers } = router.find('GET', '/hello');
handlers[0]();
//=> 'FROM GET'

router.find('POST', '/hello').handlers[0]();
//=> 'FROM POST'

router.find('DELETE', '/hello').handlers[0]();
//=> 'FROM ALL'

router.find('PUT', '/hello').handlers[0]();
//=> 'FROM ALL'
```

### trouter.METHOD(pattern, ...handlers)

This is an alias for [`trouter.add(METHOD, pattern, ...handlers)`](#trouteraddmethod-pattern-handlers), where `METHOD` is any lowercased HTTP verb.

```js
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.connect('/bar', noop);
```

### trouter.find(method, url)
Returns: `Object|Boolean`<br>
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 `handlers` keys.

* `params` &mdash; Object whose keys are the named parameters of your route pattern.
* `handlers` &mdash; Array containing the `...handlers` provided to [`.add()`](#trouteraddmethod-pattern-handlers) or [`.METHOD()`](#troutermethodpattern-handlers)


#### 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](https://lukeed.com)