From 816281dfcfb9c6fabaed9c9f5fa5e179587a5e08 Mon Sep 17 00:00:00 2001
From: yyq <kingcoon@163.com>
Date: Mon, 22 Jan 2018 18:35:05 +0800
Subject: [PATCH] middleware

---
 apps/product/index.js                  | 32 ++++----------------------------
 dispatch.js                            | 54 ++----------------------------------------------------
 doraemon/middleware/route-intercept.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 80 deletions(-)
 create mode 100644 doraemon/middleware/route-intercept.js

diff --git a/apps/product/index.js b/apps/product/index.js
index 2aacb2c..6a5236f 100644
--- a/apps/product/index.js
+++ b/apps/product/index.js
@@ -4,14 +4,15 @@
  * @date: 2016/05/06
  */
 
-var _ = require('lodash'),
-    express = require('express'),
+var express = require('express'),
     path = require('path'),
     hbsEvent = require('../../config/hbsevent');
 
 var app = express();
 var router = require('./router');
 
+var route = require('../../doraemon/middleware/route-intercept');
+
 // set view engin
 var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
 
@@ -21,32 +22,7 @@ app.on('mount', function(parent) {
     delete parent.locals.settings; // 不继承父 App 的设置
     Object.assign(app.locals, parent.locals);
 
-    if (router.rootRouter && router.rootRouter.length) {
-        let rootPath = '';
-
-        if (app.locals.rootPath && _.isString(app.locals.rootPath)) {
-            rootPath = app.locals.rootPath;
-        }
-
-        router.rootRouter.forEach(el => {
-            let fullPath = rootPath;
-
-            if (el.relatedPath && _.isString(el.relatedPath)) {
-                fullPath += el.relatedPath;
-            }
-
-            const pathArr = _.compact(fullPath.split('/'));
-
-            parent[el.method](el.path, (req, res, next) => {
-                Object.assign(res.locals, {
-                    module: pathArr[0] || res.locals.module,
-                    page: pathArr[1] || res.locals.page
-                });
-
-                return next();
-            }, ...el.args);
-        });
-    }
+    route.regist(app, parent, router);
 });
 
 app.use(global.yoho.hbs({
diff --git a/dispatch.js b/dispatch.js
index cedf2a3..2a1282e 100644
--- a/dispatch.js
+++ b/dispatch.js
@@ -4,60 +4,10 @@
  * @date: 2016/4/27
  */
 
-const _ = require('lodash');
-const express = require('express');
-const methods = require('methods');
-
-const routerHold = (app) => {
-    app.use = ((appUse) => {
-        return (...args) => {
-            let rootPath = '';
-            const arg = args[0];
-
-            if (typeof arg !== 'function') {
-                rootPath = arg;
-            }
-
-            app.locals.rootPath = rootPath || '';
-
-            return appUse.call(app, ...args);
-        };
-    })(app.use);
-
-    express.Router = ((_router) => {
-        const baseRouter = _router();
-
-        return () => {
-            let router = _router();
-
-            methods.concat('all').forEach((method) => {
-                router[method] = (path, ...args) => {
-                    const arg = args[0];
-
-                    if (typeof arg !== 'function') {
-                        args = _.drop(args);
-
-                        router.rootRouter = router.rootRouter || [];
-
-                        router.rootRouter.push({
-                            path: arg,
-                            relatedPath: path,
-                            method: method,
-                            args: args
-                        });
-                    }
-
-                    return baseRouter[method].call(router, path, ...args);
-                };
-            });
-
-            return router;
-        };
-    })(express.Router);
-};
+const route = require('./doraemon/middleware/route-intercept');
 
 module.exports = app => {
-    routerHold(app); // 拦截app.use && router
+    route.intercept(app); // 拦截app.use && router
 
     // 公共服务
     app.use('/common', require('./apps/common'));
diff --git a/doraemon/middleware/route-intercept.js b/doraemon/middleware/route-intercept.js
new file mode 100644
index 0000000..2fe3f2c
--- /dev/null
+++ b/doraemon/middleware/route-intercept.js
@@ -0,0 +1,96 @@
+/**
+ * 路由拦截
+ * @author: yyq<yanqing.yang@yoho.cn>
+ * @date: 2018/1/22
+ */
+
+const _ = require('lodash');
+const express = require('express');
+const methods = require('methods');
+
+const intercept = (app) => {
+    // 拦截调度器,获取原路径
+    app.use = ((appUse) => {
+        return (...args) => {
+            let rootPath = '';
+            const arg = args[0];
+
+            if (typeof arg !== 'function') {
+                rootPath = arg;
+            }
+
+            app.locals.rootPath = rootPath || '';
+
+            return appUse.call(app, ...args);
+        };
+    })(app.use);
+
+
+    // 拦截路由方法,解析新注册方式
+    express.Router = ((_router) => {
+        const baseRouter = _router();
+
+        return () => {
+            let router = _router();
+
+            methods.concat('all').forEach((method) => {
+                router[method] = (path, ...args) => {
+                    const arg = args[0];
+
+                    if (typeof arg !== 'function') {
+                        args = _.drop(args);
+
+                        router.rootRouter = router.rootRouter || [];
+
+                        router.rootRouter.push({
+                            path: arg,
+                            relatedPath: path,
+                            method: method,
+                            args: args
+                        });
+                    }
+
+                    return baseRouter[method].call(router, path, ...args);
+                };
+            });
+
+            return router;
+        };
+    })(express.Router);
+};
+
+
+// 注册根路由
+const regist = (app, parent, router) => {
+    if (router.rootRouter && router.rootRouter.length) {
+        let rootPath = '';
+
+        if (app.locals.rootPath && _.isString(app.locals.rootPath)) {
+            rootPath = app.locals.rootPath;
+        }
+
+        router.rootRouter.forEach(el => {
+            let fullPath = rootPath;
+
+            if (el.relatedPath && _.isString(el.relatedPath)) {
+                fullPath += el.relatedPath;
+            }
+
+            const pathArr = _.compact(fullPath.split('/'));
+
+            parent[el.method](el.path, (req, res, next) => {
+                Object.assign(res.locals, {
+                    module: pathArr[0] || res.locals.module,
+                    page: pathArr[1] || res.locals.page
+                });
+
+                return next();
+            }, ...el.args);
+        });
+    }
+};
+
+module.exports = {
+    intercept,
+    regist
+};
--
libgit2 0.24.0