Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
finish README
Browse files Browse the repository at this point in the history
  • Loading branch information
jed committed Aug 27, 2012
1 parent a3ca00e commit 20df16e
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion README.md
@@ -1,4 +1,48 @@
browserver-router
=================

A platform-agnostic router for HTTP listeners that follow the node.js spec
This is a simple and unambitious router implementation that can be used in the browser or any CommonJS environment. It has no dependencies, and weighs in at 462 minizipped bytes. It was designed for [browserver](http://browserver.org), but will work with any server that conforms to the node.js HTTP spec (in which handlers take have a `(req, res)` signature, where `req` has `url` and `method` properties, and `res` has `writeHead` and `end` methods).

Example
-------

```javascript
var router = new Router({
"/hello/:name": {
GET: function(req, res) {
res.writeHead(200)
res.end("Hello, " + req.params[0] + ".")
},

DELETE: function(req, res) {
res.writeHead(200)
res.end("Goodbye, " + req.params[0] + ".")
}
},

"/method": function(req, res) {
res.writeHead(200)
res.end("Matched method: " + req.method)
}
})
```

API
---

### router = new Router([Object routes])

Creates a new router. `routes` is optional, and can be an object where each key is a route pattern, and each value is either a route handler function, or object with methods for keys and route handler functions for values.

### router.route([String route], [Function handler])
### router.route([String route], [Object methodMap])

Adds a route to match. Both arguments are required. The `route` string is compiled into a regular expression, using the same logic as the [Backbone.js router](http://backbonejs.org/#Router-route), in which `:param` strings match a single url component between slashes and `*` splats match any number of url components. Any matching parameters are used to populate the `req.params` array by match position.

If a `handler` function is provided, it will be fired when the route is matched, for any request method.

If a `methodMap` object is provided, it will be used to disambiguate between methods of a given route by specifying them as keys, with the handlers for the values.

### router[404] and router[405]

These are where the default handlers lives. Override them to provide your own fallback logic.

0 comments on commit 20df16e

Please sign in to comment.