调整中间件的执行逻辑;调整路由的流程;
parent
170ac89ba7
commit
e3ab578088
55
Readme.md
55
Readme.md
|
@ -22,33 +22,11 @@
|
|||
* 为了方便下载安装及管理, 推荐使用 five-cli(这是一款专门为框架开发的脚本工具) 进行操作。
|
||||
|
||||
```bash
|
||||
# 全局安装 five-cli
|
||||
npm i -g @gm5/cli
|
||||
|
||||
# 进入项目目录
|
||||
cd /project/demo
|
||||
# 初始化一个项目,初始化完成会自动安装所需要的依赖
|
||||
five-cli init
|
||||
cd /project/
|
||||
# 初始化完成之后, 执行以下命令即可启动了,如果需要修改配置,可以先修改好再启动
|
||||
five-cli start
|
||||
```
|
||||
|
||||
* 也可以自行通过 npm 安装, 自己构建启动配置
|
||||
|
||||
```bash
|
||||
# 进入项目目录
|
||||
cd /project/demo
|
||||
|
||||
npm i @gm5/core
|
||||
mkdir apps public data views
|
||||
|
||||
touch app.js
|
||||
# 自行编辑app.js, 然后通过node, pm2启动项目即可
|
||||
#
|
||||
# import Five from '@gm5/core'
|
||||
# var app = new Five()
|
||||
# app.repload('./apps')
|
||||
# app.listen(8000)
|
||||
npm create five
|
||||
|
||||
```
|
||||
|
||||
|
@ -58,38 +36,26 @@ touch app.js
|
|||
建立启动文件, 如 app.js
|
||||
|
||||
```javascript
|
||||
'use strict'
|
||||
|
||||
import Five from '@gm5/core'
|
||||
import { createApp } from '@gm5/core'
|
||||
|
||||
var app = new Five()
|
||||
const app = new createApp()
|
||||
|
||||
app.set({ website: 'www.your_domain.com' })
|
||||
|
||||
// 设置域,cookie用到,不设置则等同于website
|
||||
// app.set({ domain: 'your_domain.com' })
|
||||
|
||||
// [可选], 但是要用到模板渲染页面时, 必须指定
|
||||
// app.set({ views: { enabled: true, dir: './views/'} })
|
||||
|
||||
app.preload('./apps/') // [必须], 预加载应用目录
|
||||
|
||||
app.listen(3001) // 默认是3000
|
||||
```
|
||||
|
||||
其他的配置和功能, 请参考 [文档](https://github.com/bytedo/gmf.core/wiki)。
|
||||
其他的配置和功能, 请参考 [文档](/gm5/request/wiki)。
|
||||
|
||||
|
||||
3. 启动应用。在项目根目录打开终端, 输入以下命令 `five-cli start`, 然后根据提示操作, 即可
|
||||
3. 启动应用。在项目根目录打开终端, 输入以下命令 `npm create five`, 然后根据提示操作, 即可
|
||||
|
||||
```bash
|
||||
# 初始化完成之后, 执行以下命令即可启动了,如果需要修改配置,可以先修改好再启动
|
||||
five-cli start
|
||||
|
||||
不是使用five-cli创建的项目, 可使用node/pm2等启动项目
|
||||
node app.js
|
||||
# or
|
||||
pm2 start app.js
|
||||
pm2 start app.dev.yaml
|
||||
# 正式环境执行
|
||||
pm2 start app.yaml
|
||||
```
|
||||
|
||||
|
||||
|
@ -123,6 +89,7 @@ server {
|
|||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-NginX-Proxy true;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
|
@ -130,7 +97,7 @@ server {
|
|||
proxy_max_temp_file_size 0;
|
||||
proxy_pass http://five_upstream;
|
||||
proxy_redirect off;
|
||||
proxy_read_timeout 240s;
|
||||
proxy_read_timeout 60s;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
18
index.js
18
index.js
|
@ -5,8 +5,8 @@
|
|||
*/
|
||||
|
||||
import 'es.shim' // 加载拓展方法
|
||||
import http from 'http'
|
||||
import path from 'path'
|
||||
import http from 'node:http'
|
||||
import { parse, join } from 'node:path'
|
||||
import fs from 'iofs'
|
||||
|
||||
import Request from '@gm5/request'
|
||||
|
@ -111,18 +111,18 @@ class Five {
|
|||
|
||||
// 预加载应用, 缓存以提高性能
|
||||
preload(dir) {
|
||||
var list = fs.ls(dir)
|
||||
let list = fs.ls(dir)
|
||||
|
||||
if (list) {
|
||||
list.forEach(item => {
|
||||
var { name } = path.parse(item)
|
||||
let { name } = parse(item)
|
||||
if (name.startsWith('.')) {
|
||||
return
|
||||
}
|
||||
// 如果是目录,则默认加载index.js, 其他文件不加载
|
||||
// 交由index.js自行处理, 用于复杂的应用
|
||||
if (fs.isdir(item)) {
|
||||
item = path.join(item, './index.js')
|
||||
item = join(item, './index.js')
|
||||
}
|
||||
|
||||
this.#modules[name] = import(item)
|
||||
|
@ -161,7 +161,7 @@ class Five {
|
|||
let request = new Request(req, res)
|
||||
let response = new Response(req, res)
|
||||
|
||||
if (response.rendered) {
|
||||
if (response.ended) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -184,6 +184,8 @@ class Five {
|
|||
}
|
||||
}
|
||||
|
||||
export function createApp() {
|
||||
return new Five()
|
||||
export function createApp(conf = {}) {
|
||||
let app = new Five()
|
||||
app.set(conf)
|
||||
return app
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ export function createRouter() {
|
|||
var spa = this.get('spa')
|
||||
|
||||
// 1. 先判断控制器是否存在
|
||||
if (!this.$load(spa ? 'index' : req.app)) {
|
||||
return res.error(`Controller [${req.app}] not found`, 404)
|
||||
if (!this.$load(spa ? 'index' : req.controller)) {
|
||||
return res.error(`Controller [${req.controller}] not found`, 404)
|
||||
}
|
||||
|
||||
// 2. 默认二级路由为index
|
||||
|
@ -22,23 +22,23 @@ export function createRouter() {
|
|||
}
|
||||
|
||||
// 3. 实例化控制器
|
||||
this.$load(spa ? 'index' : req.app)
|
||||
.then(async Module => {
|
||||
let mod, route, act
|
||||
this.$load(spa ? 'index' : req.controller)
|
||||
.then(async ModuleController => {
|
||||
let ctrol, route, act
|
||||
let err = ''
|
||||
|
||||
if (Module) {
|
||||
mod = new Module()
|
||||
if (ModuleController) {
|
||||
ctrol = new ModuleController()
|
||||
|
||||
readonlyProp(mod, 'context', this)
|
||||
readonlyProp(mod, 'request', req)
|
||||
readonlyProp(mod, 'response', res)
|
||||
readonlyProp(mod, 'name', req.app)
|
||||
readonlyProp(ctrol, 'context', this)
|
||||
readonlyProp(ctrol, 'request', req)
|
||||
readonlyProp(ctrol, 'response', res)
|
||||
readonlyProp(ctrol, 'name', req.controller)
|
||||
|
||||
// 4. 优先执行__main__方法
|
||||
if (mod.__main__) {
|
||||
if (ctrol.__main__) {
|
||||
try {
|
||||
let r = await mod.__main__()
|
||||
let r = await ctrol.__main__()
|
||||
if (r === false) {
|
||||
return
|
||||
}
|
||||
|
@ -48,20 +48,20 @@ export function createRouter() {
|
|||
}
|
||||
|
||||
if (spa) {
|
||||
return mod.indexAction.apply(mod, req.path)
|
||||
return ctrol.indexAction.apply(ctrol, req.path)
|
||||
} else {
|
||||
route = req.path.shift()
|
||||
act = route + 'Action'
|
||||
|
||||
if (mod[act]) {
|
||||
return mod[act].apply(mod, req.path)
|
||||
if (ctrol[act]) {
|
||||
return ctrol[act].apply(ctrol, req.path)
|
||||
} else {
|
||||
err = new Error(`Action [${route}] not found`)
|
||||
err.status = 404
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = new Error(`Controller [${req.app}] load error`)
|
||||
err = new Error(`Controller [${req.controller}] load error`)
|
||||
err.status = 500
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue