Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
yutent | 7cb670146a | |
yutent | 7646140360 | |
yutent | b9c752f8c8 | |
yutent | e436429872 | |
yutent | 79ff113b6f | |
yutent | 0cb5a56a60 | |
yutent | b1992d69af | |
yutent | 38c79587e2 | |
yutent | 2bbecc5a25 | |
yutent | 4daf60ba90 |
|
@ -11,6 +11,10 @@
|
||||||
|
|
||||||
框架要求 nodejs 版本在 12.0 或以上, 并且只支持使用`import/export`
|
框架要求 nodejs 版本在 12.0 或以上, 并且只支持使用`import/export`
|
||||||
|
|
||||||
|
## 文档
|
||||||
|
|
||||||
|
[文档](/gm5/core/wiki)
|
||||||
|
|
||||||
## 启用方法(步骤)
|
## 启用方法(步骤)
|
||||||
|
|
||||||
**注**
|
**注**
|
||||||
|
@ -47,7 +51,7 @@ app.preload('./apps/') // [必须], 预加载应用目录
|
||||||
app.listen(3001) // 默认是3000
|
app.listen(3001) // 默认是3000
|
||||||
```
|
```
|
||||||
|
|
||||||
其他的配置和功能, 请参考 [文档](/gm5/request/wiki)。
|
其他的配置和功能, 请参考 [文档](/gm5/core/wiki)。
|
||||||
|
|
||||||
|
|
||||||
3. 启动应用。在项目根目录打开终端, 输入以下命令 `npm create five`, 然后根据提示操作, 即可
|
3. 启动应用。在项目根目录打开终端, 输入以下命令 `npm create five`, 然后根据提示操作, 即可
|
||||||
|
|
|
@ -8,8 +8,8 @@ const ENV_PROD = 'production'
|
||||||
const ENV_DEV = 'development'
|
const ENV_DEV = 'development'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
db: {},
|
// 动态路由模式, 即无论是访问路径是什么, 永远只会调用 \apps\index\index()
|
||||||
spa: false, // 单路由模式, 即无论是访问路径是什么, 永远只会调用 \apps\index\index()
|
dynamic: false,
|
||||||
port: 3000,
|
port: 3000,
|
||||||
env: process.env.NODE_ENV === ENV_PROD ? ENV_PROD : ENV_DEV,
|
env: process.env.NODE_ENV === ENV_PROD ? ENV_PROD : ENV_DEV,
|
||||||
debug: process.env.NODE_ENV === ENV_DEV, // debug模式
|
debug: process.env.NODE_ENV === ENV_DEV, // debug模式
|
||||||
|
|
|
@ -11,11 +11,15 @@ export function createCors() {
|
||||||
let opts = this.get('cors')
|
let opts = this.get('cors')
|
||||||
|
|
||||||
if (opts.enabled) {
|
if (opts.enabled) {
|
||||||
let origin = req.header('origin') || req.header('referer') || ''
|
let origin = req.headers['origin'] || req.headers['referer'] || ''
|
||||||
let headers = req.header('access-control-request-headers')
|
let headers = req.headers['access-control-request-headers']
|
||||||
|
|
||||||
|
if (!origin) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
let { hostname, host, protocol } = parse(origin)
|
let { hostname, host, protocol } = parse(origin)
|
||||||
|
|
||||||
if (opts.origin.length && hostname) {
|
if (opts.origin.length) {
|
||||||
let pass = false
|
let pass = false
|
||||||
for (let it of opts.origin) {
|
for (let it of opts.origin) {
|
||||||
if (hostname.endsWith(it)) {
|
if (hostname.endsWith(it)) {
|
||||||
|
@ -24,7 +28,7 @@ export function createCors() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pass === false) {
|
if (pass === false) {
|
||||||
return res.end('')
|
return (res.body = null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (opts.credentials) {
|
if (opts.credentials) {
|
||||||
|
@ -32,7 +36,7 @@ export function createCors() {
|
||||||
}
|
}
|
||||||
|
|
||||||
res.set('Access-Control-Allow-Origin', `${protocol}//${host}`)
|
res.set('Access-Control-Allow-Origin', `${protocol}//${host}`)
|
||||||
res.set('Access-Control-Allow-Methods', req.method)
|
res.set('Access-Control-Allow-Methods', '*')
|
||||||
|
|
||||||
if (headers) {
|
if (headers) {
|
||||||
res.set('Access-Control-Allow-Headers', headers)
|
res.set('Access-Control-Allow-Headers', headers)
|
||||||
|
@ -43,7 +47,7 @@ export function createCors() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'OPTIONS') {
|
if (req.method === 'OPTIONS') {
|
||||||
return res.end('')
|
return (res.body = null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next()
|
next()
|
||||||
|
|
|
@ -9,20 +9,23 @@ import { readonlyProp } from '../lib.js'
|
||||||
export function createRouter() {
|
export function createRouter() {
|
||||||
return function (req, res, next) {
|
return function (req, res, next) {
|
||||||
let debug = this.get('debug')
|
let debug = this.get('debug')
|
||||||
let spa = this.get('spa')
|
let dynamic = this.get('dynamic')
|
||||||
|
let name = dynamic ? 'index' : req.controller
|
||||||
|
let _module = this.$load(name)
|
||||||
|
let paths = req.path.concat()
|
||||||
|
|
||||||
// 1. 先判断控制器是否存在
|
// 1. 先判断控制器是否存在
|
||||||
if (!this.$load(spa ? 'index' : req.controller)) {
|
if (!_module) {
|
||||||
return res.error(`Controller [${req.controller}] not found`, 404)
|
return res.error(`Controller [${name}] not found`, 404)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 默认二级路由为index
|
// 2. 默认二级路由为index
|
||||||
if (req.path.length < 1 && spa === false) {
|
if (paths.length < 1 && dynamic === false) {
|
||||||
req.path.push('index')
|
paths.push('index')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 实例化控制器
|
// 3. 实例化控制器
|
||||||
this.$load(spa ? 'index' : req.controller)
|
_module
|
||||||
.then(async ModuleController => {
|
.then(async ModuleController => {
|
||||||
let ctrol, route, act
|
let ctrol, route, act
|
||||||
let err = ''
|
let err = ''
|
||||||
|
@ -47,21 +50,21 @@ export function createRouter() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spa) {
|
if (dynamic) {
|
||||||
return ctrol.indexAction.apply(ctrol, req.path)
|
return ctrol.indexAction.apply(ctrol, paths)
|
||||||
} else {
|
} else {
|
||||||
route = req.path.shift()
|
route = paths.shift()
|
||||||
act = route + 'Action'
|
act = route + 'Action'
|
||||||
|
|
||||||
if (ctrol[act]) {
|
if (ctrol[act]) {
|
||||||
return ctrol[act].apply(ctrol, req.path)
|
return ctrol[act].apply(ctrol, paths)
|
||||||
} 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.controller}] load error`)
|
err = new Error(`Controller [${name}] load error`)
|
||||||
err.status = 500
|
err.status = 500
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@gm5/core",
|
"name": "@gm5/core",
|
||||||
"version": "2.0.0",
|
"version": "2.0.4",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
|
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
|
||||||
"author": "yutent <yutent.io@gmail.com>",
|
"author": "yutent <yutent.io@gmail.com>",
|
||||||
|
|
Loading…
Reference in New Issue