Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
yohobuy-node
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
1
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
yyq
7 years ago
Commit
816281dfcfb9c6fabaed9c9f5fa5e179587a5e08
1 parent
a043a6e9
middleware
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
80 deletions
apps/product/index.js
dispatch.js
doraemon/middleware/route-intercept.js
apps/product/index.js
View file @
816281d
...
...
@@ -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
({
...
...
dispatch.js
View file @
816281d
...
...
@@ -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
=>
{
route
rHold
(
app
);
// 拦截app.use && router
route
.
intercept
(
app
);
// 拦截app.use && router
// 公共服务
app
.
use
(
'/common'
,
require
(
'./apps/common'
));
...
...
doraemon/middleware/route-intercept.js
0 → 100644
View file @
816281d
/**
* 路由拦截
* @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
};
...
...
Please
register
or
login
to post a comment