core/middleware/router.js

56 lines
1.3 KiB
JavaScript

/**
* 路由
* @authors yutent (yutent@doui.cc)
* @date 2015-10-01 19:11:19
*
*/
'use strict'
export default function(req, res, next) {
if (!this.__MODULES__[req.app]) {
if (!this.__MODULES__.__error__) {
res.error(`The app [${req.app}] not found`, 404)
} else {
res.error(
this.get('debug')
? this.__MODULES__.__error__.stack
: this.__MODULES__.__error__,
500
)
}
return
}
try {
if (req.path.length < 1) {
req.path.push('index')
}
this.__MODULES__[req.app].then(Mod => {
var app = new Mod({ ctx: this, req, res })
if (this.get('routeMode') === 1) {
var act = req.path.shift()
if (app[act + 'Action']) {
app[act + 'Action'].apply(app, req.path).catch(err => {
res.error(this.get('debug') ? err.stack || err : err, 500)
})
} else {
res.error(`Action[${act}] not found`, 404)
}
} else {
if (app.indexAction) {
app.indexAction.apply(app, req.path).catch(err => {
res.error(this.get('debug') ? err.stack || err : err, 500)
})
} else {
res.error(`Default Action not found`, 404)
}
}
})
} catch (err) {
res.error(this.get('debug') ? err.stack || err : err, 500)
}
}
一个轻量级的,易学的,拓展性灵活的 nodejs MVC 框架, 5 分钟即可上手。取自"Give me five"之意, 一切就是这么简单
JavaScript 100%