当前位置:网站首页>控制器-----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文件,实现模块化
边栏推荐
猜你喜欢
随机推荐
2006年星座运势全解-射手
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
【win7】NtWaitForKeyedEvent
Invalid operator for data type.The operator is add and the type is text.
C-Eighty seven(背包+bitset)
关于MP3文件中找不到TAG标签的问题
Liunx教程超详细(完整)
数据库——概述
[Untitled] Long-term recruitment of hardware engineers-Shenzhen Baoan
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
DataFrame在指定位置插入行和列
SQL SERVER on master-slave table trigger design
导出SQLServer数据到Excel中
作为一个男人必须明白的22个道理
Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
[Structure internal power practice] Structure memory alignment (1)
SVG big fish eat small fish animation js special effects
【结构体内功修炼】结构体实现位段(二)
版本号命名规则
【结构体内功修炼】结构体内存对齐(一)









