Permalink
Browse files

0.6 README

  • Loading branch information...
1 parent 7eb6432 commit 56baa645676a3e536a49104cab970422c119f5e1 @mafintosh committed May 26, 2012
Showing with 30 additions and 15 deletions.
  1. +30 −15 README.md
View
@@ -1,51 +1,60 @@
# Router
-A lean and mean web router for [node.js](http://nodejs.org).
+
+A lean and mean http router for [node.js](http://nodejs.org).
It is available through npm:
npm install router
-The router routes using the method and a [.net](http://msdn.microsoft.com/en-us/library/cc668201.aspx) inspired pattern
+# Usage
+
+Router does one thing and one thing only - route http requests.
``` js
+var http = require('http');
var router = require('router');
-var server = router();
+var route = router();
-server.get('/', function(request, response) {
- response.writeHead(200);
- response.end('hello index page');
+route.get('/', function(req, res) {
+ res.writeHead(200);
+ res.end('hello index page');
});
-server.listen(8080); // start the server on port 8080
+http.createServer(function(req, res) {
+ route(req, res, function() {
+ res.writeHead(404);
+ res.end('no route matched...');
+ });
+}).listen(8080); // start the server on port 8080
```
If you want to grap a part of the path you can use capture groups in the pattern:
``` js
-server.get('/{base}', function(request, response) {
+route.get('/{base}', function(request, response) {
var base = request.params.base; // ex: if the path is /foo/bar, then base = foo
});
```
The capture patterns matches until the next `/` or character present after the group
``` js
-server.get('/{x}x{y}', function(request, response) {
+route.get('/{x}x{y}', function(request, response) {
// if the path was /200x200, then request.params = {x:'200', y:'200'}
});
```
Optional patterns are supported by adding a `?` at the end
``` js
-server.get('/{prefix}?/{top}', function(request, response) {
+route.get('/{prefix}?/{top}', function(request, response) {
// matches both '/a/b' and '/b'
});
```
If you want to just match everything you can use a wildcard `*` which works like unix wildcards
``` js
-server.get('/{prefix}/*', function(request, response) {
+route.get('/{prefix}/*', function(request, response) {
// matches both '/a/', '/a/b', 'a/b/c' and so on.
// the value of the wildcard is available through request.params.wildcard
});
@@ -54,18 +63,24 @@ server.get('/{prefix}/*', function(request, response) {
If the standard capture groups aren't expressive enough for you can specify an optional inline regex
``` js
-server.get('/{digits}([0-9]+)', function(request, response) {
+route.get('/{digits}([0-9]+)', function(request, response) {
// matches both '/24' and '/424' but not '/abefest' and so on.
});
```
You can also use regular expressions and the related capture groups instead:
``` js
-server.get(/^\/foo\/(\w+)/, function(request, response) {
+route.get(/^\/foo\/(\w+)/, function(request, response) {
var group = request.params[1]; // if path is /foo/bar, then group is bar
});
```
-Besides `get` the avaiable methods are `options`, `post`, `put`, `head`, `del`, `all` and `upgrade`.
-`all` matches all the standard http methods and `upgrade` is usually used for websockets.
+# Methods:
+
+* `route.get`: Match `GET` requests
+* `route.post`: Match `POST` requests
+* `route.put`: Match `PUT` requests
+* `route.head`: Match `HEAD` requests
+* `route.del`: Match `DELETE` requests
+* `route.all`: Match all above request methods.

0 comments on commit 56baa64

Please sign in to comment.