Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 162 lines (115 sloc) 2.885 kb
a577b0b @kieran Initial commit
authored
1 Barista is a simple URL router for nodejs.
2
3 Getting Barista
4 ===============
5
6 Install via npm, thusly:
7
8 ```javascript
9 npm install barista
10 ```
11
12 Using Barista
13 -------------
14
15 ```javascript
16 var Router = require('barista').Router;
17
18 var router = new Router;
19 ```
20
21
22 ### Adding routes
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
23
a577b0b @kieran Initial commit
authored
24 ```javascript
25 // a basic example
26 router.match( '/products', 'GET' )
27 .to( 'products.index' )
28
29 // Rails-style variables
30 router.match( '/products/:id', 'GET' )
31 .to( 'products.show' )
32
33 // optional parts
34 router.match( '/products/:id(.:format)', 'GET' )
35 .to( 'products.show' )
36
37 // convenience methods
38 router.get( '/products/:id(.:format)' )
39 .to( 'products.show' )
40
41 router.put( '/products/:id(.:format)' )
42 .to( 'products.update' )
43
44 router.post( '/products' )
45 .to( 'products.create' )
46
47 router.delete( '/products' )
48 .to( 'products.destroy' )
49 ```
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
50
a577b0b @kieran Initial commit
authored
51 ### REST Resources
52
53 ```javascript
54 router.resource( 'products' )
55 ```
56
57 is equivalent to:
58
59 ```javascript
60 router.get( '/products(.:format)' )
61 .to( 'products.index' )
62
63 router.get( '/products/add(.:format)' )
64 .to( 'products.add' )
65
66 router.get( '/products/:id(.:format)' )
67 .to('products.show' )
68
69 router.get('/products/:id/edit(.:format)' )
70 .to( 'products.edit' )
71
72 router.post('/products(.:format)' )
73 .to( 'products.create' )
74
75 router.put('/products/:id(.:format)' )
76 .to( 'products.update' )
77
78 router.delete('/products/:id(.:format)' )
79 .to( 'products.destroy' )
80 ```
81
82 Resolution & dispatching
83 ------------------------
84
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
85 The `router.first( url, method [, callback] )` method can be used in two ways:
a577b0b @kieran Initial commit
authored
86
87 ```javascript
88 var params = router.first( '/products/15', 'GET' )
89 ```
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
90
a577b0b @kieran Initial commit
authored
91 OR
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
92
a577b0b @kieran Initial commit
authored
93 ```javascript
94 router.first( '/products/15', 'GET', function( params ){
95 // dispatch the request or something
96 })
97 ```
98
99 You can get all the matching routes like so:
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
100
a577b0b @kieran Initial commit
authored
101 ```javascript
102 var params = router.all( '/products/15', 'GET' )
103
104 //=> [params, params, params....]
105 ```
106
107 Route generation
108 ----------------
109
110 Pass in a params hash, get back a tasty string:
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
111
a577b0b @kieran Initial commit
authored
112 ```javascript
3b1b843 @kieran Hatin' on trailing whitespace
authored
113 router.url( {
114 controller: 'products',
115 action: 'show',
a577b0b @kieran Initial commit
authored
116 id: 5
117 } )
118 //=> '/products/5'
119
120 router.url( {
3b1b843 @kieran Hatin' on trailing whitespace
authored
121 controller: 'products',
122 action: 'show',
123 id: 5,
a577b0b @kieran Initial commit
authored
124 format: 'json'
125 } )
126 //=> '/products/5.json'
127 ```
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
128
a577b0b @kieran Initial commit
authored
129 Set the optional second parameter to `true` if you want
130 extra params appended as a query string:
87f2fb5 @kieran trying to figure out GitHub Markdown...
authored
131
a577b0b @kieran Initial commit
authored
132 ```javascript
3b1b843 @kieran Hatin' on trailing whitespace
authored
133 router.url({
134 controller: 'products',
135 action: 'show',
136 id: 5,
137 format: 'json',
138 love: 'cheese'
a577b0b @kieran Initial commit
authored
139 }, true )
140 //=> '/products/5.json?love=cheese'
141 ```
142
143 Things I forgot...
144 ------------------
145 ...might be in the `/docs` folder...
146
147 ...or might not exist at all.
148
149
150 It's broken!
151 ------------
152 Shit happens.
153
154 Write a test that fails and add it to the tests folder,
155 then create an issue!
156
157 Patches welcome :-)
158
159
160 Who are you?
161 ------------
162 I'm [Kieran Huggins](mailto:kieran@refactory.ca), partner at [Refactory](http://refactory.ca) in Toronto, Canada.
Something went wrong with that request. Please try again.