Filter.js
1.49 KB
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
var path=require('path');
var util = require('util');
var md5 = require('md5');
var Emitter = require('events');
var Match = require("./Match.js");
var Filter=function(interfaces,controllers){
this.rules=[];
this.interfaces=interfaces;
this.controllers=controllers;
Emitter.call(this);
};
util.inherits(Filter,Emitter);
Filter.prototype.define = function(router,method,apis,callback) {
var me=this,guid=md5(router+":"+method);
if (typeof apis == "string") {
apis = [apis];
}
if(typeof apis=="function"){
callback=apis;
apis=[];
}
me.rules.push({
guid:guid,
router:router,
method:method
});
me.on(guid,function(){
var args=[].slice.call(arguments, 0);
var req=args[0],res=args[1];
if(!apis.length){
return callback.apply({},args);
}
var fn = function (err, interfaces, names) {
callback.apply({}, interfaces.concat(args));
};
me.interfaces.require.apply(me.interfaces,[apis, req, res, fn]);
});
};
Filter.prototype.rules = function (rules) {
};
Filter.prototype.use=function(router,method){
var me = this, rules = [], func = [];
rules = me.rules.filter(function (rule, index){
if (typeof rule.method == "string") {
rule.method = [rule.method];
}
return Match(rule.router,router)
&& !(rule.method.indexOf(method) == -1);
});
rules.forEach(function(rule){
func.push(function (req, res, next){
me.emit(rule.guid,req,res,next);
});
});
return func;
}
module.exports= Filter;