koa --- [MVC实现之四]Router、Controller、Service的实现
说明
- 上一篇: [MVC实现之三]
- 上一篇实现了,Router层、Controller层、Service层的基本原则,并且成功的通过Mar类来传递这些层级之间需要的参数.
- 这一篇主要是通过业务层面来具体实现:
- Router层:监听页面的路由,并调用Controller层的路由处理函数
- Controller层:给Router层提供服务,调用Service层提供的数据处理.
开始
打开浏览器访问: localhost:3000
- 此时并没有,路由处理函数.
- 栗子: 返回一条数据
Router层
- 思路:
1.在该层调用koa-router
2.建立路由和路由处理函数的联系
3.挂载路由
4.定义规则:将this.koa = new koa(conf)
改为this.$app = new koa(conf)
,使参数更具有语义(是一个koa的实例).
const koa = require('koa');
const koaRouter = require('koa-router');
class Mar {constructor(conf) {this.$app = new koa(conf);this.$router = new koaRouter();this.service = new Service(this);this.controller = new Controller(this);this.router = new Router(this);}
}class Router {constructor(app) {const { controller, $router, $app } = app;$router.get('/', controller.index);$app.use($router.routes());}
}
- 此时,Router层,成功的利用koa-router创建并挂载路由
- 下面只需在Controller类中写入index方法.
- koa-router,在监听路由的时候,会传入一个ctx和next对象
Controller层
- 在此层写入一个index方法,并使用ctx返回一个对象
class Controller {constructor() {const { service } = app;console.log('service:', service.test());this.service = service;}async index (ctx) {ctx.body = {name: 'marron',age: 18,remarks:`forever 18`}}
}
此时http://localhost:3000会返回如下信息:
- 调用Service层的异步方法
async index (ctx) {ctx.body = await service.getName();
}
- 重新刷新
http://localhost:3000
会报错,原因是service没有定义
- 仔细读函数会发现:
- 我们仅仅只是在
constructor
函数中使用了this.service = service
this.service = service
实际上只是把方法挂载到(Controller)实例上.- 如何在类Controller的方法中使用Service层提供的方法呢?
-3.1 在JavaScript中实际上是没有类的概念,类只不过是一个语法糖
-3.2 看以下代码
class Point {constructor() {...},getX() {...},getY() {...}
}
// 等同于
Point.prototype = {constructor() {},getX() {},getY() {}
}
得益于以上,产生灵感如下:
- 在Controller层初始化的时候,将Service方法挂载到Controller的原型上
- 在内部方法使用时,先从原型上把需要的方法取出来
改进Router层
class Controller {constructor(app) {const { service } = app;console.log('service: ', service.test());// 将service挂载Controller的原型上Controller.prototype = service;}test() {return `Controller for Router`}async index(ctx) {const service = Controller.prototype.servicectx.body = await service.index();}
}
到这里,基本满足了设计初衷:
- 通过Mar传递各层所需的参数
- 通过Router层监听路由,并把路由对应到Controller层的方法
- Controller层调用Service层的方法
Service层
- 添加index层方法
class Service {constructor(app) {console.log('Service: ok');}test() {return `Service for Controller`}async index() {return {name: 'marron',age: 18,remarks: `forever 18`}}
}
总结
- 这一篇实现了Router、Controller、Service层内的具体通信
- 下一篇将会加入Model层.并对此系列进行总结
koa --- [MVC实现之四]Router、Controller、Service的实现相关推荐
- koa --- [MVC实现之二]Controller层的实现
[MVC实现之一]传送门 https://blog.csdn.net/piano9425/article/details/103362966 Router层 router这一层,不做业务处理,仅仅只是 ...
- koa --- [MVC实现之五]Model层的实现
说明 上一篇: MVC实现之四 这一篇主要介绍: 项目中用到的Sequelize库中的一些方法,参考使用Sequelize连接mysql 将Model层加入Mar类中 Service层 还是从业务出发 ...
- koa --- [MVC实现之三]换个角度重新开始-初始化
说明 下面文章是对该系列前面2篇及项目中经验的总结,重新开始写的 实现了Mar类,贯穿Router层.Controller层.Service层 基本骨架的搭建 初始 使用Koa创建一个简单的服务器,一 ...
- ASP.NET MVC中ApiController与Controller的区别
本文翻译自:Difference between ApiController and Controller in ASP.NET MVC I've been playing around with A ...
- Spring MVC 在JSP中获取 Service或Dao
Spring MVC 在JSP中获取service 在Controller.Service层都已经配置好了自动注入,但是在JSP中直接使用 IuserInfoShService uishService ...
- 解释@Component @Controller @Service @Repository
2019独角兽企业重金招聘Python工程师标准>>> Spring 配置管理 Bean XML .net 对Spring的注解标签刚刚接触,所以就找了几个常用的,记录下,感觉注解用 ...
- 如何解决Asp.Net MVC和WebAPI的Controller名称不能相同的问题
如何解决Asp.Net MVC和WebAPI的Controller名称不能相同的问题 参考文章: (1)如何解决Asp.Net MVC和WebAPI的Controller名称不能相同的问题 (2)ht ...
- php lmpl,tjx-cold: 用于根据配置模板,快速生成controller,service,serviceimpl 代码
用于根据配置模板,快速生成controller,service,serviceimpl 代码(交流群 623169994 ) 为什么要开发这款插件 市面上有很多基于数据库生成代码的工具,但是我自己的工 ...
- @Controller,@Service,@Repository,@Component详解
转载自 @Controller,@Service,@Repository,@Component详解 @Controller 用来表示一个web控制层bean,如SpringMvc中的控制器. @Ser ...
最新文章
- 经典的Java基础面试题集锦
- linux命令:返回上一次目录
- 【Web】JavaWeb项目为什么我们要放弃jsp?为什么要前后端解耦?为什么要前后端分离?2.0版,为分布式架构打基础。 - CSDN博客
- python高阶函数filter_Python进阶系列连载(13)——Python内置高阶函数filter(上)...
- AndroidMVP
- 微软低代码工具 Power Apps 配置不当,暴露3800万条数据记录
- Python3控制安卓手机快手极速版持续金币收益
- ubuntu编译安装PHP5.6 ipArchive支持 configure: error: system libzip must be upgraded to version = 0.11
- YOLO测试图片显示准确率值
- 基于正则表达式的JavaScript/C++语法高亮(js版)
- PC端微信登录实现流程
- 今日头条——校招在线编程题《头条校招》
- 计算机通电后自动断电,电脑开机自动断电,详细教您电脑开机自动断电怎么解决...
- MIC灵敏度, MIC动态范围下限值估计, -3dB, dB加减
- 钢琴软件c语言源代码,使用C语言编写钢琴小程序
- linux时间同步命令centos6,CentOS6.5搭建NTP服务器,并为思科交换机提供时间同步
- 关于google是如何搜索的论文
- Visio2007使用技巧
- 公共盘(网络盘)打开慢
- 微信公众平台-网页授权域名 遇到的坑
热门文章
- winform 鼠标 静止时间_赢得电竞的奥秘,你需要一块好鼠标垫
- python if name main 的作用_Python中if __name__ == __main__: 的作用
- GPU Gems2 - 9 S.T.A.L.K.E.R.中的延迟着色(Deferred Shading in S.T.A.L.K.E.R.)
- 汇编语言属于C语言吧,汇编语言和c语言的区别是什么
- amd为什么还用针脚_为什么AMD不取消cpu上的针脚?
- Openpose——windows编译(炒鸡简单)
- 【caffe-Windows】微软官方caffe之 matlab接口配置
- Qt与FFmpeg联合开发指南(二)——解码(2):封装和界面设计
- mysql在linux下的安装(5.7版本以后)
- 新手求大神,有其他swit-case的思路写这个程序么?