說明
- 下面文章是對該系列前面2篇及項目中經驗的總結,重新開始寫的
- 實現了Mar類,貫穿Router層、Controller層、Service層
- 基本骨架的搭建
初始
- 使用Koa創建一個簡單的服務器,一般會使用如下
const koa = require('koa');
const app = new koa();
const Router = require('koa-router');
const router =new Router();
const handleIndex = async ctx => {ctx.body = 'Hello World';
};
const handleListen = async port => {console.log(`[Mar]Server is running at http://localhost:${port}`);
}
router.get('/', handleIndex);
app.use(router.routes());
app.listen(3000, handleListen(3000));
原則
借助Egg的Controller,Service的引用,有了以下靈感:
- 自定義一個類(Mar),在創建實例(app)的時候,會將Controller、Service方法掛載到app上
- 即Mar類是,集中管理各層的初始化及存放通用方法
- Controller層,主要用于聯系路由層和Service層
- Service層在操作數據之后,給Controller層提供服務.
設計Mar類
根據上述的原則,我們設計Mar類的初始如下:
const koa = require('koa');class Mar {constructor(conf) {const router = new Router();const controller = new Controller();const service = new Service();this.koa = new koa(conf); // 相當于appthis.router = router.initRouter();this.controller = controller.initController();this.service = service.initService();// this.koa.use(router.routes());}listen(port) {this.koa.listen(port, async () => {console.log(`[mar]Server is running at http://localhost:${port}`);})}
}class Router {constructor(conf) {}initRouter() {console.log('initRouter');}
}class Controller {constructor(conf) {}initController() {console.log('initController');}
}
class Service {constructor(conf) {}initService() {console.log('initService');}
}module.exports = Mar
說明:
1.現在都寫在一個Mar里面,后期代碼肯定很多,會分開寫,然后將模塊引入.
2.init函數僅用于測試,后期會根據需求更改
3.此時Mar類的引用如下.
// index.js
const Mar = require('./mar');
const mar = new Mar();
mar.listen(3000);
命令行運行: node index.js
,結果如下
改進Mar類
- 上面測試代碼跑通
- 開始逐步完善原則
- Router層負責聯系,路由和路由處理事件
- Controller負責路由處理事件
- Service負責處理復雜的方法,然后提供給Controller層
- Mar貫通各個類,負責存儲通用方法,并將參數傳遞給各個層,相當于一個粘合劑貫穿整個流程
[注: 在contructor中,使用this相當于指向實例]
- 改進Mar類如下:
const koa = require('koa');class Mar {constructor(conf) {this.koa = new koa(conf); // 相當于appthis.service = new Service(this);this.controller = new Controller(this.service);this.router = new Router(this.controller);}listen(port) {this.koa.listen(port, async () => {console.log(`[mar]Server is running at http://localhost:${port}`);})}
}class Router {constructor(controller) {console.log('Router:', controller.test());}
}
class Controller {constructor(service) {console.log('controller:', service.test());}test() {return 'Controller for Router'}
}
class Service {constructor(app) {console.log('Service:', app);}test() {return 'Service for Controller'}
}module.exports = Mar
運行后輸出如下:
說明:
- 可以看到在Service層,使用(暫時還未寫)外面的服務
- 在Controller層使用Service層的服務
- 在Router層使用Controller層的服務
進一步改進Mar類
- 引用分層的目的,是為了使開發更有規范,便于以后自己和他人的閱讀.
- 最終目的是提高開發效率,分為近期的和長久的
- 一些通用的功能,就必須提取出來,放在Mar類里面
- 上面在在傳遞服務給相鄰層的時候,并未傳入通用方法,因此改變如下
const koa = require('koa');class Mar {constructor(conf) {this.koa = new koa(conf); // 相當于appthis.service = new Service(this); this.controller = new Controller(this);this.router = new Router(this);}listen(port) {this.koa.listen(port, async () => {console.log(`[mar]Server is running at http://localhost:${port}`);})}
}class Router {constructor(app) {const { controller } = app;console.log('Router:', controller.test());}
}
class Controller {constructor(app) {const { service } = app;console.log('controller:', service.test());}test() {return 'Controller for Router'}
}
class Service {constructor(app) {console.log('Service:', app);}test() {return 'Service for Controller'}
}module.exports = Mar
- 注意: 由于引用了分層,故在Router層盡量的不要使用Service方法.
總結
- 這一篇主要實現了一個基本骨架
- 下一篇將會實現Router層和Controller層、Controller層和Service層的具體業務實現