Compare commits

..

8 Commits

Author SHA1 Message Date
yutent 7cb670146a 2.0.4 2024-07-10 09:25:46 +08:00
yutent 7646140360 简化路由代码 2024-07-01 10:41:40 +08:00
yutent b9c752f8c8 简化路由代码 2024-07-01 10:34:58 +08:00
yutent e436429872 Merge branch 'master' of git.wkit.fun:gm5/core 2024-06-28 09:59:50 +08:00
yutent 79ff113b6f 优化跨域逻辑 2024-06-27 18:38:01 +08:00
yutent 0cb5a56a60 Update package.json 2024-02-23 20:54:45 +08:00
yutent b1992d69af Update middleware/cors.js 2024-02-23 20:54:26 +08:00
yutent 38c79587e2 修复跨域中间件 2023-11-08 09:33:12 +08:00
3 changed files with 23 additions and 16 deletions

View File

@ -11,11 +11,15 @@ export function createCors() {
let opts = this.get('cors') let opts = this.get('cors')
if (opts.enabled) { if (opts.enabled) {
let origin = req.header('origin') || req.header('referer') || '' let origin = req.headers['origin'] || req.headers['referer'] || ''
let headers = req.header('access-control-request-headers') let headers = req.headers['access-control-request-headers']
if (!origin) {
return next()
}
let { hostname, host, protocol } = parse(origin) let { hostname, host, protocol } = parse(origin)
if (opts.origin.length && hostname) { if (opts.origin.length) {
let pass = false let pass = false
for (let it of opts.origin) { for (let it of opts.origin) {
if (hostname.endsWith(it)) { if (hostname.endsWith(it)) {
@ -24,7 +28,7 @@ export function createCors() {
} }
} }
if (pass === false) { if (pass === false) {
return res.end('') return (res.body = null)
} }
} }
if (opts.credentials) { if (opts.credentials) {
@ -32,7 +36,7 @@ export function createCors() {
} }
res.set('Access-Control-Allow-Origin', `${protocol}//${host}`) res.set('Access-Control-Allow-Origin', `${protocol}//${host}`)
res.set('Access-Control-Allow-Methods', req.method) res.set('Access-Control-Allow-Methods', '*')
if (headers) { if (headers) {
res.set('Access-Control-Allow-Headers', headers) res.set('Access-Control-Allow-Headers', headers)
@ -43,7 +47,7 @@ export function createCors() {
} }
if (req.method === 'OPTIONS') { if (req.method === 'OPTIONS') {
return res.end('') return (res.body = null)
} }
} }
next() next()

View File

@ -10,19 +10,22 @@ export function createRouter() {
return function (req, res, next) { return function (req, res, next) {
let debug = this.get('debug') let debug = this.get('debug')
let dynamic = this.get('dynamic') let dynamic = this.get('dynamic')
let name = dynamic ? 'index' : req.controller
let _module = this.$load(name)
let paths = req.path.concat()
// 1. 先判断控制器是否存在 // 1. 先判断控制器是否存在
if (!this.$load(dynamic ? 'index' : req.controller)) { if (!_module) {
return res.error(`Controller [${req.controller}] not found`, 404) return res.error(`Controller [${name}] not found`, 404)
} }
// 2. 默认二级路由为index // 2. 默认二级路由为index
if (req.path.length < 1 && dynamic === false) { if (paths.length < 1 && dynamic === false) {
req.path.push('index') paths.push('index')
} }
// 3. 实例化控制器 // 3. 实例化控制器
this.$load(dynamic ? 'index' : req.controller) _module
.then(async ModuleController => { .then(async ModuleController => {
let ctrol, route, act let ctrol, route, act
let err = '' let err = ''
@ -48,20 +51,20 @@ export function createRouter() {
} }
if (dynamic) { if (dynamic) {
return ctrol.indexAction.apply(ctrol, req.path) return ctrol.indexAction.apply(ctrol, paths)
} else { } else {
route = req.path.shift() route = paths.shift()
act = route + 'Action' act = route + 'Action'
if (ctrol[act]) { if (ctrol[act]) {
return ctrol[act].apply(ctrol, req.path) return ctrol[act].apply(ctrol, paths)
} else { } else {
err = new Error(`Action [${route}] not found`) err = new Error(`Action [${route}] not found`)
err.status = 404 err.status = 404
} }
} }
} else { } else {
err = new Error(`Controller [${req.controller}] load error`) err = new Error(`Controller [${name}] load error`)
err.status = 500 err.status = 500
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/core", "name": "@gm5/core",
"version": "2.0.1", "version": "2.0.4",
"type": "module", "type": "module",
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手", "description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",