add spa mode

v1
yutent 2022-07-04 16:04:29 +08:00
parent e36cc37719
commit 2dfba0da88
3 changed files with 19 additions and 13 deletions

View File

@ -27,6 +27,7 @@ export default {
level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip
secret: 'it_is_secret_key' // jwt密钥, 使用时请修改
},
spa: false, // 单路由模式, 即无论是访问路径是什么, 永远只会调用 \apps\index\index()
website: 'localhost',
domain: '', // cookie域, 默认等于website
port: 3000,

View File

@ -6,22 +6,23 @@
export default function (req, res, next) {
var debug = this.get('debug')
var spa = this.get('spa')
// 1. 先判断控制器是否存在
if (!this.__MODULES__[req.app]) {
if (!this.__MODULES__[spa ? 'index' : req.app]) {
return res.error(`Controller [${req.app}] not found`, 404)
}
// 2. 默认二级路由为index
if (req.path.length < 1) {
if (req.path.length < 1 && spa === false) {
req.path.push('index')
}
// 3. 实例化控制器
this.__MODULES__[req.app]
this.__MODULES__[spa ? 'index' : req.app]
.then(async ({ default: Mod }) => {
var app
var err = ''
let app, route, act
let err = ''
if (Mod) {
app = new Mod()
@ -36,15 +37,19 @@ export default function (req, res, next) {
}
}
var route = req.path.shift()
var act = route + 'Action'
if (spa) {
return app.indexAction.apply(app, req.path)
} else {
route = req.path.shift()
act = route + 'Action'
if (app[act]) {
return app[act].apply(app, req.path)
} else {
err = new Error(`Route [${route}] not found`)
err = new Error(`Action [${route}] not found`)
err.status = 404
}
}
} else {
err = new Error(`Controller [${req.app}] load error`)
err.status = 500

View File

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