当前位置:网站首页>控制器-----controller
控制器-----controller
2022-08-05 08:06:00 【cjx177187】
Controller负责解析用户的输入,处理后返回响应的结果。1.所有的Controller 文件都必须放在 app/controller目录下2.支持多级目录,访问时可以通过目录名级联访问。
作用:
- 接受用户的提供的参数,然后从数据库中查找参数返回给用户
- 根据用户访问的URl不同然后渲染不同的模板
- 代理服务:将用户的请求返回给其他服务器
模板语法:
//严格模式
'use strict';
//引入egg中Controller属性
const Controller = require('egg').Controller;
//继承Controller这个类
class HomeController extends Controller {
async index() {
const { ctx } = this;//上下问对象
ctx.body = 'hi, egg';
}
}
//导出Controller
module.exports = HomeController;
如果路由是:
router.get('/hello', controller.user.news)
则控制器对应:
在controller文件夹下有一个user文件中有一个news方法
如果路由是:
router.get('/hello', controller.user.home.news)
则控制器对应:
在controller文件夹下有一个user文件夹下有一个home文件中有一个news方法
anyn 函数中可以输入任意类型的数据,他会自己转成JSON数据
this.ctx就是controller提供的功能主要是使用它提供的:给前端发送数据,访问插件功能
this.ctx.body="hello"====>只会执行一次就断开连接
可以再controller文件夹中新建新的js文件,实现模块化
边栏推荐
猜你喜欢
随机推荐
Redis缓存以及存在的问题--缓存穿透、缓存雪崩、缓存击穿及解决方法
C-Eighty seven(背包+bitset)
执子之手,与子偕老。你同意么?
U++ 创建UI
Use of thread pool (combined with Future/Callable)
[Repost] Marry a man must marry a man whose salary is at least 3571.4 yuan higher than yours
JVM运行流程,运行时数据区,类加载,垃圾回收,JMM解析
Green Apple Forum reopens
环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
Basic introduction of stack and queue and C language implementation of functions such as creation, destruction, entry and exit, counting the number of elements, viewing elements, etc., as well as stac
php向mysql写入数据失败
本地能ping通虚拟机,虚拟机ping不通本地
【结构体内功修炼】枚举和联合的奥秘(三)
Version number naming convention
VXE-Table融合多语言
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
TRACE32——通用寄存器查看与修改
风控特征的优化分箱,看看这样教科书的操作
别把你的天使弄丢了
TRACE32——C源码关联1









