Skip to content

Commit

Permalink
Release v1.0.7 动态路由文件支持导入任意函数形式。
Browse files Browse the repository at this point in the history
原来导入的动态路由文件只能是Router,现在取消了此限制,只要是函数都可以导入,以方便中间件,或者是一些不是Router的RequestHandler的使用。

1.0.7
  • Loading branch information
Starrah committed Jul 5, 2020
1 parent b8205b9 commit 92c4cc0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-router-dynamic",
"version": "1.0.6",
"version": "1.0.7",
"description": "A router used for load more routers based on directory structure.",
"main": "src/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ class DynamicRouter {
return null
let module = ((require('./' + Path.relative(__dirname, targetFile).replace(/\\/g, '/'))))
module = module?.default || module
// 如果是Router类型的对象,就返回路由
// 如果是函数类型的对象,就返回路由
// 这里注意判断router所用的express原型应与app的保持一致(否则可能由于express版本不同导致出问题)
if (module && module.__proto__ == req.app._router.__proto__)
if (module && typeof module == "function")
return module as express.Router
//否则继续处理
}
Expand Down

0 comments on commit 92c4cc0

Please sign in to comment.