Installations
npm install barista
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 0.4.0
NPM Version
1.4.28
Score
74.9
Supply Chain
98.1
Quality
75.7
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (56.36%)
CoffeeScript (43.35%)
Makefile (0.29%)
Developer
Download Statistics
Total Downloads
186,452
Last Day
14
Last Week
66
Last Month
245
Last Year
2,778
GitHub Statistics
110 Stars
102 Commits
22 Forks
9 Watching
2 Branches
8 Contributors
Bundle Size
20.87 kB
Minified
6.74 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.5.3
Package Id
barista@0.5.3
Size
373.57 kB
NPM Version
1.4.28
Total Downloads
Cumulative downloads
Total Downloads
186,452
Last day
-12.5%
14
Compared to previous day
Last week
17.9%
66
Compared to previous week
Last month
-54.6%
245
Compared to previous month
Last year
41.6%
2,778
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
2
Barista is a simple URL router for NodeJS.
In a nutshell
1router.get( '/:beverage/near/:location(.:format)' ) 2 .to( 'beverage.byLocation' ) 3 4router.first( '/coffee/near/90210', 'GET' ) 5// -> { controller:'beverage', action:'byLocation', beverage:'coffee', location:90210 } 6 7router.url({ 8 controller: 'beverage', 9 action: 'byLocation', 10 beverage: 'coffee', 11 location: 90210, 12 format: 'json' 13}) 14// -> '/coffee/near/90210.json'
Getting Barista
Install via npm, thusly:
1npm install barista
Using Barista
1var Router = require('barista').Router; 2 3var router = new Router;
Adding routes
A simple example
1router.match( '/products', 'GET' ) 2 .to( 'products.index' )
Rails-esque variable names
1router.match( '/products/:id', 'GET' ) 2 .to( 'products.show' ) 3 4router.match( '/profiles/:username', 'GET' ) 5 .to( 'users.show' ) 6 7router.match( '/products/:id(.:format)', 'GET' ) 8 .to( 'products.show' )
Globs (they also capture slashes)
1router.get('/timezones/*tzname') 2 .to( 'timezones.select' ) 3 4router.first( '/timezones/America/Toronto', 'GET' ) 5// -> { controller:'timezones', action:'select', tzname:'America/Toronto' } 6 7 8router.match( '/*path(.:format)' ) // a "catch-all" route: 9 .to( 'errors.notFound' ) 10 11router.first( '/somewhere/that/four-oh-fours.json', 'GET' ) 12// -> { controller:'errors', action:'notFound', path:'somewhere/that/four-oh-fours', format:'json' }
Match conditions
1router.match( '/:beverage/near/:zipcode', 'GET' ) 2 .to( 'beverage.byZipCode' ) 3 .where({ 4 // an array of options 5 beverage: [ 'coffee', 'tea', 'beer', 'warm_sake' ], 6 // a regex pattern 7 zipcode: /^\d{5}(-\d{4})?$/ 8 }) 9 10router.match( '/:beverage/near/:location', 'GET' ) 11 .to( 'beverage.byLocation' ) 12 .where({ 13 // could be a postal code 14 // OR a zip code 15 // OR the word 'me' (geolocation FTW) 16 location: [ /^\d{5}(-\d{4})?$/, /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/, 'me' ] 17 })
Convenience methods
1router.get( '/products/:id(.:format)' ) 2 .to( 'products.show' ) 3 4router.put( '/products/:id(.:format)' ) 5 .to( 'products.update' ) 6 7router.post( '/products' ) 8 .to( 'products.create' ) 9 10router.del( '/products' ) 11 .to( 'products.destroy' ) 12 13router.options( '/products' ) 14 .to( 'products.options' )
REST Resources
1router.resource( 'products' )
is equivalent to:
1router.get( '/products(.:format)' ) 2 .to( 'products.index' ) 3 4router.get( '/products/add(.:format)' ) 5 .to( 'products.add' ) 6 7router.get( '/products/:id(.:format)' ) 8 .to('products.show' ) 9 10router.get('/products/:id/edit(.:format)' ) 11 .to( 'products.edit' ) 12 13router.post('/products(.:format)' ) 14 .to( 'products.create' ) 15 16router.put('/products/:id(.:format)' ) 17 .to( 'products.update' ) 18 19router.del('/products/:id(.:format)' ) 20 .to( 'products.destroy' )
Removing Routes
In some cases, you will need to remove routes on a running router. The router.remove( name )
method will work for this, but requires
use of the otherwise unused route.name( name )
method.
Adding a name (currently only used with this functionality)
1router.match( '/products/:id', 'GET' ) 2 .to( 'products.show' ) 3 .name('products_show')
Removing a named route
1 2router.remove('products_show') 3
Resolution & dispatching
The router.first( url, method [, callback] )
method can be used in two ways:
1var params = router.first( '/products/15', 'GET' )
OR
1router.first( '/products/15', 'GET', function( err, params ){ 2 if (err) { 3 // couldn't find match 4 } 5 // dispatch the request or something 6})
You can get all the matching routes like so:
1var params = router.all( '/products/15', 'GET' ) 2 3//=> [params, params, params....]
Route generation
Pass in a params hash, get back a tasty string:
1router.url( { 2 controller: 'products', 3 action: 'show', 4 id: 5 5} ) 6//=> '/products/5' 7 8router.url( { 9 controller: 'products', 10 action: 'show', 11 id: 5, 12 format: 'json' 13} ) 14//=> '/products/5.json'
Set the optional second parameter to true
if you want
extra params appended as a query string:
1router.url({ 2 controller: 'products', 3 action: 'show', 4 id: 5, 5 format: 'json', 6 love: 'cheese' 7}, true ) 8//=> '/products/5.json?love=cheese'
TODOs
- Add namespace support
- Better support for named routes
- Customizable resources
Things I forgot...
...might be in the /docs
folder...
...or might not exist at all.
It's broken!
Shit happens.
Write a test that fails and add it to the tests folder, then create an issue!
Patches welcome :-)
Who are you?
I'm Kieran Huggins in Toronto, Canada.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.txt:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.txt:0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Score
3
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More