From 2dfba0da884f77c75ae07e5c8f6f549eb90199f2 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 4 Jul 2022 16:04:29 +0800 Subject: [PATCH] add spa mode --- config/index.js | 1 + middleware/router.js | 29 +++++++++++++++++------------ package.json | 2 +- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/config/index.js b/config/index.js index 0732211..f6462f3 100644 --- a/config/index.js +++ b/config/index.js @@ -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, diff --git a/middleware/router.js b/middleware/router.js index 96d13d7..29941ab 100644 --- a/middleware/router.js +++ b/middleware/router.js @@ -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,14 +37,18 @@ export default function (req, res, next) { } } - var route = req.path.shift() - var act = route + 'Action' - - if (app[act]) { - return app[act].apply(app, req.path) + if (spa) { + return app.indexAction.apply(app, req.path) } else { - err = new Error(`Route [${route}] not found`) - err.status = 404 + route = req.path.shift() + act = route + 'Action' + + if (app[act]) { + return app[act].apply(app, req.path) + } else { + err = new Error(`Action [${route}] not found`) + err.status = 404 + } } } else { err = new Error(`Controller [${req.app}] load error`) diff --git a/package.json b/package.json index c273a67..e152478 100644 --- a/package.json +++ b/package.json @@ -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 ",