v2
yutent 2023-10-24 15:24:55 +08:00
parent 04069523d3
commit 26bc8622d2
4 changed files with 65 additions and 43 deletions

View File

@ -51,8 +51,8 @@ export default {
origin: [], // ['abc.com', 'a.foo.com'] origin: [], // ['abc.com', 'a.foo.com']
maxAge: 14400 maxAge: 14400
}, },
regexp: {
// 常用正则 // 常用正则
regexp: {
email: /^[\w\.\-]+@\w+([\.\-]\w+)*\.\w+$/, email: /^[\w\.\-]+@\w+([\.\-]\w+)*\.\w+$/,
uname: /^[A-Za-z\d_]{4,16}$/, uname: /^[A-Za-z\d_]{4,16}$/,
passwd: /^\S{6,20}$/, passwd: /^\S{6,20}$/,

View File

@ -32,14 +32,12 @@ process.on('uncaughtException', err => {
}) })
export default class Five { export default class Five {
constructor() { #config = config
hideProperty(this, '__FIVE__', config) #modules = {}
hideProperty(this, '__MODULES__', {}) #middlewares = [Cors]
hideProperty(this, '__MIDDLEWARE__', [Cors])
}
__main__() { #start() {
var { domain, website, session, jwt } = this.__FIVE__ var { domain, website, session, jwt } = this.#config
domain = domain || website domain = domain || website
session.domain = session.domain || domain session.domain = session.domain || domain
@ -53,12 +51,12 @@ export default class Five {
// 以便用户自定义的中间件可以直接操作session // 以便用户自定义的中间件可以直接操作session
if (session.enabled) { if (session.enabled) {
this.install(sessionPackage) this.install(sessionPackage)
this.__MIDDLEWARE__.unshift(sessionConnect) this.#middlewares.unshift(sessionConnect)
} }
// 开启jwt // 开启jwt
if (jwt) { if (jwt) {
this.install(jwtPackage) this.install(jwtPackage)
this.__MIDDLEWARE__.unshift(jwtConnect) this.#middlewares.unshift(jwtConnect)
} }
// 路由中间件要在最后 // 路由中间件要在最后
@ -71,17 +69,17 @@ export default class Five {
set(obj) { set(obj) {
for (let i in obj) { for (let i in obj) {
if (typeof obj[i] === 'object' && !Array.isArray(obj[i])) { if (typeof obj[i] === 'object' && !Array.isArray(obj[i])) {
if (!this.__FIVE__[i]) { if (!this.#config[i]) {
this.__FIVE__[i] = obj[i] this.#config[i] = obj[i]
} else { } else {
try { try {
Object.assign(this.__FIVE__[i], obj[i]) Object.assign(this.#config[i], obj[i])
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
} }
} else { } else {
this.__FIVE__[i] = obj[i] this.#config[i] = obj[i]
} }
} }
return this return this
@ -89,7 +87,7 @@ export default class Five {
// 获取全局配置 // 获取全局配置
get(key) { get(key) {
return this.__FIVE__[key] return this.#config[key]
} }
// 加载中间件 // 加载中间件
@ -98,7 +96,7 @@ export default class Five {
// 而是框架内部封装过的,可通过origin属性访问原生的对象 // 而是框架内部封装过的,可通过origin属性访问原生的对象
use(fn) { use(fn) {
if (typeof fn === 'function') { if (typeof fn === 'function') {
this.__MIDDLEWARE__.push(fn) this.#middlewares.push(fn)
return this return this
} }
throw TypeError('argument must be a function') throw TypeError('argument must be a function')
@ -127,7 +125,7 @@ export default class Five {
item = path.join(item, './index.js') item = path.join(item, './index.js')
} }
this.__MODULES__[name] = import(item).catch(err => { this.#modules[name] = import(item).catch(err => {
console.error(err) console.error(err)
return { default: null } return { default: null }
}) })
@ -137,18 +135,30 @@ export default class Five {
return this return this
} }
$load(name) {
return this.#modules[name]
}
// 启动http服务 // 启动http服务
listen(port) { listen(port) {
this.set({ port })
return this
}
run() {
var _this = this var _this = this
var server var server
this.__main__() this.#start()
server = http.createServer(function (req, res) { server = http.createServer()
server
.on('request', function (req, res) {
var request = new Request(req, res) var request = new Request(req, res)
var response = new Response(req, res) var response = new Response(req, res)
var middleware = _this.__MIDDLEWARE__.concat() var middleware = _this.#middlewares.concat()
var fn = middleware.shift() var fn = middleware.shift()
if (response.rendered) { if (response.rendered) {
@ -168,9 +178,21 @@ export default class Five {
})() })()
} }
}) })
.on('listening', _ => {
console.log('启动成功, 可通过以下地址访问')
console.log(' 本地: %s://%s:%d', 'http', '127.0.0.1', this.get('port'))
})
.on('error', err => {
console.error(err)
})
server.listen(port || this.get('port')) server.listen(this.get('port'))
return server this.server = server
return this
} }
} }
export function createApp() {
return new Five()
}

View File

@ -9,7 +9,7 @@ export default function (req, res, next) {
var spa = this.get('spa') var spa = this.get('spa')
// 1. 先判断控制器是否存在 // 1. 先判断控制器是否存在
if (!this.__MODULES__[spa ? 'index' : req.app]) { if (!this.$load(spa ? 'index' : req.app)) {
return res.error(`Controller [${req.app}] not found`, 404) return res.error(`Controller [${req.app}] not found`, 404)
} }
@ -19,7 +19,7 @@ export default function (req, res, next) {
} }
// 3. 实例化控制器 // 3. 实例化控制器
this.__MODULES__[spa ? 'index' : req.app] this.$load(spa ? 'index' : req.app)
.then(async ({ default: Mod }) => { .then(async ({ default: Mod }) => {
let app, route, act let app, route, act
let err = '' let err = ''

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/core", "name": "@gm5/core",
"version": "1.1.5", "version": "2.0.0",
"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>",