{ "name": "router", "version": "0.6.2", "description": "A lean and mean http router", "keywords": [ "connect", "middleware", "router", "route", "http" ], "repository": { "type": "git", "url": "git://github.com/gett/router.git" }, "contributors": [ { "name": "Mathias Buus Madsen", "email": "m@ge.tt" }, { "name": "Ian Jorgensen" } ], "dependencies": {}, "readme": "# Router\n\nA lean and mean http router for [node.js](http://nodejs.org). \nIt is available through npm:\n\n\tnpm install router\n\t\n## Usage\n\nRouter does one thing and one thing only - route http requests.\n\n``` js\nvar http = require('http');\nvar router = require('router');\nvar route = router();\n\nroute.get('/', function(req, res) {\n\tres.writeHead(200);\n\tres.end('hello index page');\n});\n\nhttp.createServer(route).listen(8080); // start the server on port 8080\n```\n\nIf you want to grap a part of the path you can use capture groups in the pattern:\n\n``` js\nroute.get('/{base}', function(req, res) {\n\tvar base = req.params.base; // ex: if the path is /foo/bar, then base = foo\n});\n```\n\nThe capture patterns matches until the next `/` or character present after the group\n\n``` js\nroute.get('/{x}x{y}', function(req, res) {\n\t// if the path was /200x200, then req.params = {x:'200', y:'200'}\n});\n```\n\nOptional patterns are supported by adding a `?` at the end\n\n``` js\nroute.get('/{prefix}?/{top}', function(req, res) {\n\t// matches both '/a/b' and '/b'\n});\n```\n\nIf you want to just match everything you can use a wildcard `*` which works like unix wildcards\n\n``` js\nroute.get('/{prefix}/*', function(req, res) {\n\t// matches both '/a/', '/a/b', 'a/b/c' and so on.\n\t// the value of the wildcard is available through req.params.wildcard\n});\n```\n\nIf the standard capture groups aren't expressive enough for you can specify an optional inline regex \n\n``` js\nroute.get('/{digits}([0-9]+)', function(req, res) {\n\t// matches both '/24' and '/424' but not '/abefest' and so on.\n});\n```\n\nYou can also use regular expressions and the related capture groups instead:\n\n``` js\nroute.get(/^\\/foo\\/(\\w+)/, function(req, res) {\n\tvar group = req.params[1]; // if path is /foo/bar, then group is bar\n});\n```\n\n## Methods\n\n* `route.get`: Match `GET` requests\n* `route.post`: Match `POST` requests\n* `route.put`: Match `PUT` requests\n* `route.head`: Match `HEAD` requests \n* `route.del`: Match `DELETE` requests\n* `route.options`: Match `OPTIONS` requests\n* `route.all`: Match all above request methods.\n\n## Error handling\n\nBy default Router will return 404 if you no route matched. If you want to do your own thing you can give it a callback:\n\n``` js\nroute(req, res, function() {\n\t// no route was matched\n\tres.writeHead(404);\n\tres.end();\n});\n```\n\nYou can also provide a catch-all to a given route that is called if no route was matched:\n\n``` js\nroute.get(function(req, res) {\n\t// called if no other get route matched\n\tres.writeHead(404);\n\tres.end('no GET handler found');\n});\n```", "readmeFilename": "README.md", "_id": "router@0.6.2", "_from": "router@" }