当前位置:网站首页>控制器-----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文件,实现模块化
边栏推荐
- DeFi 前景展望:概览主流 DeFi 协议二季度进展
- 支持触屏slider轮播插件
- DataFrame在指定位置插入行和列
- Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
- TRACE32——外设寄存器查看与修改
- 【每日一题】1403. 非递增顺序的最小子序列
- Redis implements distributed lock-principle-detailed explanation of the problem
- TRACE32——List源代码查看
- 关于MP3文件中找不到TAG标签的问题
- 爱情是一部忧伤的乐曲
猜你喜欢
随机推荐
TRACE32——加载符号表信息用于调试
外企Office常用英语
高端无主灯设计灯光设计该如何布置射灯灯具?
Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
TensorFlow installation steps
程序设计中的感悟
Chapter 12 贝叶斯网络
TRACE32——通用寄存器查看与修改
国家强制性灯具安全标准GB7000.1-2015
创业者如何吸引风险投资商
剑指Offer面试题解总结1-10
Jmeter永久设置中文界面
MobileNetV1架构解析
Fiddler tool explanation
SVG big fish eat small fish animation js special effects
监听浏览器刷新操作
达梦数据库大表添加字段
SVG Star Wars Style Toggle Toggle Button
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
Mysql 死锁和死锁的解决方案









