Skip to content
Browse files

use onrequest method instead of instance

  • Loading branch information...
1 parent 0babecf commit e5d833a2cb47cb81c2a627d249241eb6d6ae156d @jed committed
Showing with 37 additions and 37 deletions.
  1. +6 −6 README.md
  2. +23 −23 index.js
  3. +8 −8 test.js
View
12 README.md
@@ -20,7 +20,7 @@ Example
```javascript
var Router = require("router")
-var router = new Router({
+var router = Router({
"/salutation/:name": {
GET: function(req, res) {
res.writeHead(200)
@@ -44,7 +44,7 @@ var router = new Router({
})
var http = require("http")
-var server = http.createServer(router.onrequest)
+var server = http.createServer(router)
server.listen(8000)
```
@@ -52,7 +52,7 @@ server.listen(8000)
### In the browser with [browserver](http://browserver.org) and [engine.io](https://github.com/LearnBoost/engine.io)
```javascript
-var router = new Router({
+var router = Router({
"/salutation/:name": {
GET: function(req, res) {
res.writeHead(200)
@@ -75,7 +75,7 @@ var router = new Router({
}
})
-var server = http.createServer(router.onrequest)
+var server = http.createServer(router)
var ws = new eio.Socket({host: "myserver.com"})
server.listen(ws)
@@ -86,10 +86,10 @@ API
### router = new Router([Object routes])
-Creates a new router. If a `routes` object is passed, it will `.route(key, value)` will be called for each key.
+Creates a new router, which is a function with the standard `(req, res)` signature. If a `routes` object is passed, it will `.route(key, value)` will be called for each key.
```javascript
-var router = new Router({
+var router = Router({
"/": {
GET: function(req, res) {
res.writeHead(200)
View
46 index.js
@@ -1,11 +1,5 @@
function Router(routes) {
- var route, router = this
-
- this.routes = []
-
- for (route in routes) this.route(route, routes[route])
-
- this.onrequest = function(req, res) {
+ function router(req, res) {
var i = 0, route, error
while (route = router.routes[i++]) {
@@ -37,27 +31,33 @@ function Router(routes) {
error.statusCode = 404
router.onerror(error, res)
}
-}
-Router.prototype.route = function(route, methods) {
- route = route
- .replace(/[-[\]{}()+?.,\\^$|#\s]/g , "\\$&" )
- .replace(/:\w+/g , "([^\/]+)")
- .replace(/\*\w+/g , "(.*?)" )
+ router.routes = []
- if (typeof methods == "function") methods = {"*": methods}
+ router.route = function(route, methods) {
+ route = route
+ .replace(/[-[\]{}()+?.,\\^$|#\s]/g , "\\$&" )
+ .replace(/:\w+/g , "([^\/]+)")
+ .replace(/\*\w+/g , "(.*?)" )
- this.routes.push({
- pattern: new RegExp("^" + route + "$"),
- methods: methods
- })
+ if (typeof methods == "function") methods = {"*": methods}
- return this
-}
+ router.routes.push({
+ pattern: new RegExp("^" + route + "$"),
+ methods: methods
+ })
+
+ return router
+ }
+
+ router.onerror = function(err, res) {
+ res.writeHead(err.statusCode || 500, {"Content-Type": "text/plain"})
+ res.end(err.message || "Internal Server Error")
+ }
+
+ for (var route in routes) router.route(route, routes[route])
-Router.prototype.onerror = function(err, res) {
- res.writeHead(err.statusCode || 500, {"Content-Type": "text/plain"})
- res.end(err.message || "Internal Server Error")
+ return router
}
if (typeof require == "function" && typeof module != "undefined") {
View
16 test.js
@@ -1,7 +1,7 @@
var assert = require("assert")
var Router = require("./")
-var router = new Router({
+var router = Router({
"/": {
GET: function(req, res) {
res.writeHead(200)
@@ -34,7 +34,7 @@ router.route("/nomethod", function(req, res) {
var called = false
-router.onrequest(
+router(
{url: "/", method: "GET"},
{
writeHead: function(code){ assert.equal(code, 200) },
@@ -48,7 +48,7 @@ router.onrequest(
assert(called)
called = false
-router.onrequest(
+router(
{url: "/hello/dude", method: "GET"},
{
writeHead: function(code){ assert.equal(code, 200) },
@@ -62,7 +62,7 @@ router.onrequest(
assert(called)
called = false
-router.onrequest(
+router(
{url: "/", method: "PATCH"},
{
writeHead: function(code){ assert.equal(code, 405) },
@@ -76,7 +76,7 @@ router.onrequest(
assert(called)
called = false
-router.onrequest(
+router(
{url: "/notfound", method: "GET"},
{
writeHead: function(code){ assert.equal(code, 404) },
@@ -90,7 +90,7 @@ router.onrequest(
assert(called)
called = false
-router.onrequest(
+router(
{url: "/nomethod", method: "PATCH"},
{
writeHead: function(code){ assert.equal(code, 200) },
@@ -104,7 +104,7 @@ router.onrequest(
assert(called)
called = false
-router.onrequest(
+router(
{url: "/throws", method: "GET"},
{
writeHead: function(code){ assert.equal(code, 500) },
@@ -118,7 +118,7 @@ router.onrequest(
assert(called)
called = false
-router.onrequest(
+router(
{url: "/nothrow", method: "GET"},
{
writeHead: function(code){ assert.equal(code, 204) },

0 comments on commit e5d833a

Please sign in to comment.
Something went wrong with that request. Please try again.