Authored by htoooth

add doc

@@ -21,6 +21,11 @@ files.forEach((file) => { @@ -21,6 +21,11 @@ files.forEach((file) => {
21 } 21 }
22 }); 22 });
23 23
  24 +/**
  25 + * 1. origin 可接受是 正则 和 函数
  26 + * 2. target 可接受是 匹配字符串 和 函数
  27 + * @returns {Function}
  28 + */
24 module.exports = () => { 29 module.exports = () => {
25 return (req, res, next) => { 30 return (req, res, next) => {
26 let curDomainRules = domainRules[req.subdomains[0]]; 31 let curDomainRules = domainRules[req.subdomains[0]];
@@ -32,6 +37,7 @@ module.exports = () => { @@ -32,6 +37,7 @@ module.exports = () => {
32 for (let i = 0; i < curDomainRules.length; i++) { 37 for (let i = 0; i < curDomainRules.length; i++) {
33 let rule = curDomainRules[i]; 38 let rule = curDomainRules[i];
34 39
  40 + // 匹配成功
35 if ( 41 if (
36 (_.isRegExp(rule.origin) && !_.isEmpty(rule.origin.exec(req.url))) || 42 (_.isRegExp(rule.origin) && !_.isEmpty(rule.origin.exec(req.url))) ||
37 (_.isFunction(rule.origin) && rule.origin(req)) 43 (_.isFunction(rule.origin) && rule.origin(req))
@@ -39,15 +45,21 @@ module.exports = () => { @@ -39,15 +45,21 @@ module.exports = () => {
39 let newUrl = req.url; 45 let newUrl = req.url;
40 46
41 if (_.isRegExp(rule.origin)) { 47 if (_.isRegExp(rule.origin)) {
  48 + // 正则
42 if (_.isFunction(rule.target)) { 49 if (_.isFunction(rule.target)) {
  50 + // 函数
43 newUrl = req.url.replace(rule.origin, _.partial(rule.target, req)); 51 newUrl = req.url.replace(rule.origin, _.partial(rule.target, req));
44 } else if (_.isString(rule.target)) { 52 } else if (_.isString(rule.target)) {
  53 + // 字符串
45 newUrl = req.url.replace(rule.origin, req.target); 54 newUrl = req.url.replace(rule.origin, req.target);
46 } 55 }
47 } else if (_.isFunction(rule.origin)) { 56 } else if (_.isFunction(rule.origin)) {
  57 + // 函数
48 if (_.isFunction(rule.target)) { 58 if (_.isFunction(rule.target)) {
  59 + // 函数
49 newUrl = rule.target(req); 60 newUrl = rule.target(req);
50 } else if (_.isString(rule.target)) { 61 } else if (_.isString(rule.target)) {
  62 + // 字符串
51 newUrl = rule.target; 63 newUrl = rule.target;
52 } 64 }
53 } 65 }