方法调整
parent
2803e35311
commit
f89259a20f
|
@ -0,0 +1,30 @@
|
||||||
|
declare module '@gm5/core' {
|
||||||
|
import { Server } from 'http'
|
||||||
|
|
||||||
|
interface Middleware {
|
||||||
|
(req: Request, res: Response, next: () => void): void
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Installable {
|
||||||
|
name: string
|
||||||
|
install: (args: any) => any
|
||||||
|
}
|
||||||
|
|
||||||
|
class Five {
|
||||||
|
get server(): Server
|
||||||
|
|
||||||
|
set(obj: Config): this
|
||||||
|
|
||||||
|
get(key: string): any
|
||||||
|
|
||||||
|
use(fn: Middleware): this
|
||||||
|
|
||||||
|
install(module: Installable, args: any): this
|
||||||
|
|
||||||
|
preload(dir: string): this
|
||||||
|
|
||||||
|
listen(port?: number, callback?: () => void): this
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createApp(conf?: object): Five
|
||||||
|
}
|
61
index.js
61
index.js
|
@ -28,11 +28,15 @@ class Five {
|
||||||
#middlewares = [createCors()]
|
#middlewares = [createCors()]
|
||||||
|
|
||||||
#server = null
|
#server = null
|
||||||
|
#online = false
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
readonlyProp(this, 'state', Object.create(null))
|
readonlyProp(this, 'state', Object.create(null))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 循环顺序执行中间件, 直到执行完所有中间件或没有调用next
|
||||||
|
*/
|
||||||
async #loop(req, res, idx = 0) {
|
async #loop(req, res, idx = 0) {
|
||||||
let fn = this.#middlewares[idx]
|
let fn = this.#middlewares[idx]
|
||||||
if (fn) {
|
if (fn) {
|
||||||
|
@ -43,6 +47,12 @@ class Five {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注入实例化对象到实例池中
|
||||||
|
#install({ name, install }, args) {
|
||||||
|
this['$$' + name] = install.call(this, args)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
get server() {
|
get server() {
|
||||||
|
@ -76,23 +86,34 @@ class Five {
|
||||||
return this.#config[key]
|
return this.#config[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载中间件
|
/**
|
||||||
// 与别的中间件用法有些不一样, 回调的传入参数中的req和res,
|
* 加载中间件
|
||||||
// 并非原生的request对象和response对象,
|
* 与别的中间件用法有些不一样, 回调的传入参数中的req和res,
|
||||||
// 而是框架内部封装过的,可通过origin属性访问原生的对象
|
* 并非原生的request对象和response对象,
|
||||||
use(fn = noop) {
|
* 而是框架内部封装过的,可通过origin属性访问原生的对象
|
||||||
if (typeof fn === 'function') {
|
* @param {*} middleware
|
||||||
this.#middlewares.push(fn)
|
* @returns Five
|
||||||
return this
|
*/
|
||||||
|
use(middleware = noop, args) {
|
||||||
|
if (this.#online) {
|
||||||
|
return console.error('Server already started, cannot use middleware')
|
||||||
}
|
}
|
||||||
throw TypeError('argument must be a function')
|
if (typeof middleware === 'function') {
|
||||||
|
this.#middlewares.push(middleware)
|
||||||
|
} else if (typeof middleware === 'object' && typeof middleware.install === 'function') {
|
||||||
|
this.#install(middleware, args)
|
||||||
|
} else {
|
||||||
|
throw TypeError('argument must be a function or installable object')
|
||||||
|
}
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注入实例化对象到实例池中
|
/**
|
||||||
// 与use方法不同的是, 这个会在server创建之前就已经执行
|
* 注入中间件, 与use方法不同, install方法会立即执行
|
||||||
install({ name, install }, args) {
|
* @deprecated 请使用use方法, 将在下个版本中移除
|
||||||
this['$$' + name] = install.call(this, args)
|
*/
|
||||||
return this
|
install(middleware, args) {
|
||||||
|
return this.#install(middleware, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 预加载应用, 缓存以提高性能
|
// 预加载应用, 缓存以提高性能
|
||||||
|
@ -128,12 +149,11 @@ class Five {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动http服务
|
// 启动http服务
|
||||||
listen(port) {
|
listen(port = 3000, callback = noop) {
|
||||||
this.set({ port })
|
this.set({ port })
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
run() {
|
this.#online = true
|
||||||
|
|
||||||
this.#server = http.createServer()
|
this.#server = http.createServer()
|
||||||
|
|
||||||
// 路由中间件要在最后
|
// 路由中间件要在最后
|
||||||
|
@ -151,14 +171,15 @@ class Five {
|
||||||
.on('listening', _ => {
|
.on('listening', _ => {
|
||||||
if (this.get('debug')) {
|
if (this.get('debug')) {
|
||||||
console.log('Server successfully started ...')
|
console.log('Server successfully started ...')
|
||||||
console.log('%s://%s:%d\n', 'http', '127.0.0.1', this.get('port'))
|
console.log('%s://%s:%d\n', 'http', '127.0.0.1', port)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('error', err => {
|
.on('error', err => {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
})
|
})
|
||||||
.listen(this.get('port'))
|
.listen(port)
|
||||||
|
|
||||||
|
callback.call(this, this.#server)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
|
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
|
||||||
"author": "yutent <yutent.io@gmail.com>",
|
"author": "yutent <yutent.io@gmail.com>",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
"types": "index.d.ts",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@gm5/session": "^2.0.0",
|
"@gm5/session": "^2.0.0",
|
||||||
"@gm5/request": "^2.0.3",
|
"@gm5/request": "^2.0.3",
|
||||||
|
|
Loading…
Reference in New Issue