-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
65 lines (54 loc) · 1.53 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
/**
* Module dependencies.
*/
var express = require('express')
, methods = require('methods').concat('del')
, apps = express.application
? [express.application]
: [express.HTTPServer.prototype, express.HTTPSServer.prototype];
/**
* Namespace using the given `path`, providing a callback `fn()`,
* which will be invoked immediately, resetting the namespace to the previous.
*
* @param {String} path
* @param {Function} fn
* @return {Server} for chaining
* @api public
*/
var namespace = exports.namespace = function(){
var args = Array.prototype.slice.call(arguments)
, path = args.shift()
, fn = args.pop()
, self = this;
if (args.length) self.all(path, args);
if (args.length) self.all(path + '/*', args);
(this._ns = this._ns || []).push(path);
fn.call(this);
this._ns.pop();
return this;
};
apps.forEach(function(app){
app.namespace = namespace;
});
/**
* Proxy HTTP methods to provide namespacing support.
*/
methods.forEach(function(method){
apps.forEach(function(app){
var orig = app[method];
app[method] = function(val){
var len = arguments.length;
if ('get' == method && 1 == len) return orig.call(this, val);
var args = Array.prototype.slice.call(arguments)
, path = args.shift()
, self = this;
this.namespace(path, function(){
path = this._ns.join('/').replace(/\/\//g, '/').replace(/\/$/, '') || '/';
args.forEach(function(fn){
orig.call(self, path, fn);
});
});
return this;
};
});
});