Skip to content

Commit 72b6c99

Browse files
committed
add use feature
1 parent d1e943c commit 72b6c99

File tree

6 files changed

+115
-38
lines changed

6 files changed

+115
-38
lines changed

build/http.js

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/router.js

Lines changed: 61 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ var interceptorOne = function (done, next) {
99
console.log(this.request.agent);
1010
};
1111

12-
http.on('/', interceptorOne, function (done) {
12+
http.on('/', function (done) {
1313
return done('json', this.text + this.request.method);
14-
});
14+
}).use(interceptorOne);
15+
16+
http.use(interceptorOne);
1517

1618
http.assets('/src', __dirname + '/../src');
1719

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-tiny-http",
3-
"version": "1.2.7",
3+
"version": "1.2.8",
44
"description": "A tiny node.js http framework",
55
"main": "build/http.js",
66
"scripts": {

src/http.coffee

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ module.exports =
1818
# register result
1919
result: Result.register
2020

21+
22+
# use default functions
23+
use: Router.use
24+
2125

2226
# on method
23-
on: (pattern, fn...) ->
24-
Router.register null, pattern, fn
27+
on: (pattern, fn, method = null) ->
28+
Router.register method, pattern, fn
2529

2630

2731
# get method
28-
get: (pattern, fn...) ->
32+
get: (pattern, fn) ->
2933
Router.register 'GET', pattern, fn
3034

3135

3236
# post method
33-
post: (pattern, fn...) ->
37+
post: (pattern, fn) ->
3438
Router.register 'POST', pattern, fn
3539

3640

src/router.coffee

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Result = require './result'
55
# routes map
66
routes = {}
77

8+
# default functions
9+
defaults = []
10+
811

912
# create match uri pattern
1013
match = (method, pattern) ->
@@ -35,14 +38,35 @@ match = (method, pattern) ->
3538

3639

3740
# register routes
38-
register = (method, pattern, actions) ->
41+
register = (method, pattern, fn) ->
3942
tester = match method, pattern
4043
functions = []
41-
44+
pushed = no
45+
46+
routes[pattern] =
47+
get: ->
48+
if not pushed
49+
functions.push fn
50+
pushed = yes
51+
52+
[tester, functions]
53+
use: (actions...) ->
54+
for action in actions
55+
if action instanceof Array
56+
for item in action
57+
functions.push item
58+
else
59+
functions.push action
60+
61+
62+
# register default functions
63+
use = (actions...) ->
4264
for action in actions
43-
functions.push action if action not in functions
44-
45-
routes[pattern] = [tester, functions]
65+
if action instanceof Array
66+
for item in action
67+
defaults.push item
68+
else
69+
defaults.push action
4670

4771

4872
# handler for http
@@ -63,7 +87,7 @@ handler = (result, options) ->
6387
response.respond() if not response.responded
6488

6589
for pattern, def of routes
66-
[tester, functions] = def
90+
[tester, functions] = def.get()
6791
params = {}
6892
index = -1
6993

@@ -73,13 +97,13 @@ handler = (result, options) ->
7397

7498
do next = ->
7599
index += 1
76-
fn = functions[index]
100+
fn = if index >= defaults.length then functions[index - defaults.length] else defaults[index]
77101
fn.call context, done, next if fn?
78102

79103
return
80104

81105
done 'notFound'
82106

83107

84-
module.exports = { register, handler }
108+
module.exports = { register, handler, use }
85109

0 commit comments

Comments
 (0)