Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
yutent | 7cb670146a | |
yutent | 7646140360 | |
yutent | b9c752f8c8 | |
yutent | e436429872 | |
yutent | 79ff113b6f |
|
@ -13,9 +13,13 @@ export function createCors() {
|
|||
if (opts.enabled) {
|
||||
let origin = req.headers['origin'] || req.headers['referer'] || ''
|
||||
let headers = req.headers['access-control-request-headers']
|
||||
|
||||
if (!origin) {
|
||||
return next()
|
||||
}
|
||||
let { hostname, host, protocol } = parse(origin)
|
||||
|
||||
if (opts.origin.length && hostname) {
|
||||
if (opts.origin.length) {
|
||||
let pass = false
|
||||
for (let it of opts.origin) {
|
||||
if (hostname.endsWith(it)) {
|
||||
|
|
|
@ -10,19 +10,22 @@ export function createRouter() {
|
|||
return function (req, res, next) {
|
||||
let debug = this.get('debug')
|
||||
let dynamic = this.get('dynamic')
|
||||
let name = dynamic ? 'index' : req.controller
|
||||
let _module = this.$load(name)
|
||||
let paths = req.path.concat()
|
||||
|
||||
// 1. 先判断控制器是否存在
|
||||
if (!this.$load(dynamic ? 'index' : req.controller)) {
|
||||
return res.error(`Controller [${req.controller}] not found`, 404)
|
||||
if (!_module) {
|
||||
return res.error(`Controller [${name}] not found`, 404)
|
||||
}
|
||||
|
||||
// 2. 默认二级路由为index
|
||||
if (req.path.length < 1 && dynamic === false) {
|
||||
req.path.push('index')
|
||||
if (paths.length < 1 && dynamic === false) {
|
||||
paths.push('index')
|
||||
}
|
||||
|
||||
// 3. 实例化控制器
|
||||
this.$load(dynamic ? 'index' : req.controller)
|
||||
_module
|
||||
.then(async ModuleController => {
|
||||
let ctrol, route, act
|
||||
let err = ''
|
||||
|
@ -48,20 +51,20 @@ export function createRouter() {
|
|||
}
|
||||
|
||||
if (dynamic) {
|
||||
return ctrol.indexAction.apply(ctrol, req.path)
|
||||
return ctrol.indexAction.apply(ctrol, paths)
|
||||
} else {
|
||||
route = req.path.shift()
|
||||
route = paths.shift()
|
||||
act = route + 'Action'
|
||||
|
||||
if (ctrol[act]) {
|
||||
return ctrol[act].apply(ctrol, req.path)
|
||||
return ctrol[act].apply(ctrol, paths)
|
||||
} else {
|
||||
err = new Error(`Action [${route}] not found`)
|
||||
err.status = 404
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = new Error(`Controller [${req.controller}] load error`)
|
||||
err = new Error(`Controller [${name}] load error`)
|
||||
err.status = 500
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@gm5/core",
|
||||
"version": "2.0.3",
|
||||
"version": "2.0.4",
|
||||
"type": "module",
|
||||
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
|
||||
"author": "yutent <yutent.io@gmail.com>",
|
||||
|
|
Loading…
Reference in New Issue