diff --git a/index.js b/index.js index 879140b..31b6af1 100644 --- a/index.js +++ b/index.js @@ -24,7 +24,7 @@ process.on('uncaughtException', err => { class Five { #config = config - #modules = {} + #controllers = {} #middlewares = [createCors()] #server = null @@ -67,7 +67,7 @@ class Five { try { let { default: Module } = await import(item) - this.#modules[name] = Module + this.#controllers[name] = Module } catch (err) { console.error(err) } @@ -149,7 +149,7 @@ class Five { } $load(name) { - return this.#modules[name] + return this.#controllers[name] } // 启动http服务 @@ -158,13 +158,15 @@ class Five { return console.error('Server already started') } + // 路由中间件要在最后 + this.use(createRouter()) + + this.set({ port }) this.#online = true this.#server = http.createServer() - // 路由中间件要在最后 - this.use(createRouter()) this.#server .on('request', (req, res) => { diff --git a/middleware/router.js b/middleware/router.js index 0d41a5f..bc89bd5 100644 --- a/middleware/router.js +++ b/middleware/router.js @@ -7,15 +7,15 @@ import { readonlyProp } from '../lib.js' export function createRouter() { - return function (req, res, next) { + return async 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 ModuleController = this.$load(name) let paths = req.path.concat() // 1. 先判断控制器是否存在 - if (!_module) { + if (!ModuleController) { return res.error(`Controller [${name}] not found`, 404) } @@ -25,54 +25,46 @@ export function createRouter() { } // 3. 实例化控制器 - _module - .then(async ModuleController => { - let ctrol, route, act - let err = '' + try { + let route, act + let err = '' - if (ModuleController) { - ctrol = new ModuleController() + let ctrol = new ModuleController() - readonlyProp(ctrol, 'context', this) - readonlyProp(ctrol, 'request', req) - readonlyProp(ctrol, 'response', res) - readonlyProp(ctrol, 'name', req.controller) + readonlyProp(ctrol, 'context', this) + readonlyProp(ctrol, 'request', req) + readonlyProp(ctrol, 'response', res) + readonlyProp(ctrol, 'name', req.controller) - // 4. 优先执行__main__方法 - if (ctrol.__main__) { - try { - let r = await ctrol.__main__() - if (r === false) { - return - } - } catch (err) { - return Promise.reject(err) - } + // 4. 优先执行__main__方法 + if (ctrol.__main__) { + try { + let r = await ctrol.__main__() + if (r === false) { + return } - - if (dynamic) { - return ctrol.indexAction.apply(ctrol, paths) - } else { - route = paths.shift() - act = route + 'Action' - - if (ctrol[act]) { - return ctrol[act].apply(ctrol, paths) - } else { - err = new Error(`Action [${route}] not found`) - err.status = 404 - } - } - } else { - err = new Error(`Controller [${name}] load error`) - err.status = 500 + } catch (err) { + return Promise.reject(err) } + } - return Promise.reject(err) - }) - .catch(err => { - console.error(err) - res.error(debug ? err.stack || err : err, err.status || 500) - }) + if (dynamic) { + return ctrol.indexAction.apply(ctrol, paths) + } else { + route = paths.shift() + act = route + 'Action' + + if (ctrol[act]) { + return ctrol[act].apply(ctrol, paths) + } else { + err = new Error(`Action [${route}] not found`) + err.status = 404 + } + } + + res.error(debug ? err.stack || err : err, err.status || 500) + } catch (err) { + console.error(err) + } } }