路由模式开关改名

master
yutent 2023-11-02 17:10:51 +08:00
parent 67a63bf67e
commit 4daf60ba90
3 changed files with 8 additions and 8 deletions

View File

@ -8,8 +8,8 @@ const ENV_PROD = 'production'
const ENV_DEV = 'development' const ENV_DEV = 'development'
export default { export default {
db: {}, // 动态路由模式, 即无论是访问路径是什么, 永远只会调用 \apps\index\index()
spa: false, // 单路由模式, 即无论是访问路径是什么, 永远只会调用 \apps\index\index() dynamic: false,
port: 3000, port: 3000,
env: process.env.NODE_ENV === ENV_PROD ? ENV_PROD : ENV_DEV, env: process.env.NODE_ENV === ENV_PROD ? ENV_PROD : ENV_DEV,
debug: process.env.NODE_ENV === ENV_DEV, // debug模式 debug: process.env.NODE_ENV === ENV_DEV, // debug模式

View File

@ -9,20 +9,20 @@ import { readonlyProp } from '../lib.js'
export function createRouter() { export function createRouter() {
return function (req, res, next) { return function (req, res, next) {
let debug = this.get('debug') let debug = this.get('debug')
let spa = this.get('spa') let dynamic = this.get('dynamic')
// 1. 先判断控制器是否存在 // 1. 先判断控制器是否存在
if (!this.$load(spa ? 'index' : req.controller)) { if (!this.$load(dynamic ? 'index' : req.controller)) {
return res.error(`Controller [${req.controller}] not found`, 404) return res.error(`Controller [${req.controller}] not found`, 404)
} }
// 2. 默认二级路由为index // 2. 默认二级路由为index
if (req.path.length < 1 && spa === false) { if (req.path.length < 1 && dynamic === false) {
req.path.push('index') req.path.push('index')
} }
// 3. 实例化控制器 // 3. 实例化控制器
this.$load(spa ? 'index' : req.controller) this.$load(dynamic ? 'index' : req.controller)
.then(async ModuleController => { .then(async ModuleController => {
let ctrol, route, act let ctrol, route, act
let err = '' let err = ''
@ -47,7 +47,7 @@ export function createRouter() {
} }
} }
if (spa) { if (dynamic) {
return ctrol.indexAction.apply(ctrol, req.path) return ctrol.indexAction.apply(ctrol, req.path)
} else { } else {
route = req.path.shift() route = req.path.shift()

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/core", "name": "@gm5/core",
"version": "2.0.0", "version": "2.0.1",
"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>",