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