修复路由中间件

master
yutent 2025-01-03 18:54:52 +08:00
parent a4df422623
commit edce8b8cd6
2 changed files with 45 additions and 51 deletions

View File

@ -24,7 +24,7 @@ process.on('uncaughtException', err => {
class Five { class Five {
#config = config #config = config
#modules = {} #controllers = {}
#middlewares = [createCors()] #middlewares = [createCors()]
#server = null #server = null
@ -67,7 +67,7 @@ class Five {
try { try {
let { default: Module } = await import(item) let { default: Module } = await import(item)
this.#modules[name] = Module this.#controllers[name] = Module
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
@ -149,7 +149,7 @@ class Five {
} }
$load(name) { $load(name) {
return this.#modules[name] return this.#controllers[name]
} }
// 启动http服务 // 启动http服务
@ -158,13 +158,15 @@ class Five {
return console.error('Server already started') return console.error('Server already started')
} }
// 路由中间件要在最后
this.use(createRouter())
this.set({ port }) this.set({ port })
this.#online = true this.#online = true
this.#server = http.createServer() this.#server = http.createServer()
// 路由中间件要在最后
this.use(createRouter())
this.#server this.#server
.on('request', (req, res) => { .on('request', (req, res) => {

View File

@ -7,15 +7,15 @@
import { readonlyProp } from '../lib.js' import { readonlyProp } from '../lib.js'
export function createRouter() { export function createRouter() {
return function (req, res, next) { return async 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 name = dynamic ? 'index' : req.controller
let _module = this.$load(name) let ModuleController = this.$load(name)
let paths = req.path.concat() let paths = req.path.concat()
// 1. 先判断控制器是否存在 // 1. 先判断控制器是否存在
if (!_module) { if (!ModuleController) {
return res.error(`Controller [${name}] not found`, 404) return res.error(`Controller [${name}] not found`, 404)
} }
@ -25,54 +25,46 @@ export function createRouter() {
} }
// 3. 实例化控制器 // 3. 实例化控制器
_module try {
.then(async ModuleController => { let route, act
let ctrol, route, act let err = ''
let err = ''
if (ModuleController) { let ctrol = new ModuleController()
ctrol = new ModuleController()
readonlyProp(ctrol, 'context', this) readonlyProp(ctrol, 'context', this)
readonlyProp(ctrol, 'request', req) readonlyProp(ctrol, 'request', req)
readonlyProp(ctrol, 'response', res) readonlyProp(ctrol, 'response', res)
readonlyProp(ctrol, 'name', req.controller) readonlyProp(ctrol, 'name', req.controller)
// 4. 优先执行__main__方法 // 4. 优先执行__main__方法
if (ctrol.__main__) { if (ctrol.__main__) {
try { try {
let r = await ctrol.__main__() let r = await ctrol.__main__()
if (r === false) { if (r === false) {
return return
}
} catch (err) {
return Promise.reject(err)
}
} }
} catch (err) {
if (dynamic) { return Promise.reject(err)
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
} }
}
return Promise.reject(err) if (dynamic) {
}) return ctrol.indexAction.apply(ctrol, paths)
.catch(err => { } else {
console.error(err) route = paths.shift()
res.error(debug ? err.stack || err : err, err.status || 500) 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)
}
} }
} }