-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
112 lines (95 loc) · 2.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*!
* Railstyle Router v1.5.0
* Copyright(c) 2012 wǒ_is神仙 <[email protected]>
* MIT Licensed
*/
var express = require('express').application
var Router = require('./lib/router')
var util = require('./lib/utils')
var METHODS = Router._methods
var ACTIONS = Router._actions
/**
* Resource routing allows you to quickly declare all of the common routes for
* a given resourceful controller.
*
* app.resource[s](name, [options], [callback])
*
* options:
* path -> Allows you to change the path prefix for the resource.
* only -> Only generate routes for the given actions.
* except -> Generate all routes except for the given actions.
*/
Array.prototype.concat('resources', 'resource').forEach(function(api) {
express[api] = function(name, options, callback) {
if (util.isFunction(options)) {
callback = options
options = {}
}
options || (options = {})
var router = new Router(this, name, options, api === 'resources')
util.isFunction(callback) && callback.call(router)
// chain
return router
}
})
/**
* Matches a url pattern to one or more routes.
*
* app.match('path', 'namespace/controller#action', 'methods')
*/
express['match'] = function(path, to, via) {
var i = to.lastIndexOf('#')
if (i === -1) {
return false
}
var j = to.lastIndexOf('/')
var namespace = j !== -1 && to.slice(0, j)
var controller = to.slice(j + 1, i)
var action = to.slice(i + 1)
var app = this
var actions = require(util.setController(app, namespace, controller))
var args = util.combo(app, action, actions['before_filter'], actions['skip_before_filter'])
args.unshift(path)
args.push(
util.createCallback(actions[action], namespace, controller, action)
)
if (via = util.toArray(via)) {
via.forEach(function(method) {
app[method].apply(app, args)
})
return false
}
switch (i = ACTIONS.indexOf(action)) {
case -1:
METHODS.forEach(function(method) {
app[method].apply(app, args)
})
break
case 4:
case 5:
case 6:
app[METHODS[i - 3]].apply(app, args)
break
default:
app.get.apply(app, args)
}
}
/**
* Scopes routes to a specific namespace.
*
* app.namespace('path', function() {
* this.resources()
* this.resource()
* ...
* })
*/
express['namespace'] = function(path, callback) {
var router = new Router(this, null, {namespace: path})
this.routesMap[path + '/'] = {}
util.isFunction(callback) && callback.call(router)
// chain
return router
}
express['routesMap'] = {
'/': {}
}