当前位置:网站首页>Controller-----controller
Controller-----controller
2022-08-05 08:10:00 【cjx177187】
Controller负责解析用户的输入,处理后返回响应的结果.1.所有的Controller 文件都必须放在 app/controller目录下2.支持多级目录,访问时可以通过目录名级联访问.
作用:
- Accepts parameters provided by the user,Then look up the parameters from the database and return them to the user
- 根据用户访问的URlDifferent then renders a different template
- 代理服务:Return the user's request to other servers
模板语法:
//严格模式
'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)
The controller corresponds:
在controller文件夹下有一个user文件中有一个news方法
如果路由是:
router.get('/hello', controller.user.home.news)
The controller corresponds:
在controller文件夹下有一个user文件夹下有一个home文件中有一个news方法
anyn Any type of data can be entered into the function,He will convert himselfJSON数据
this.ctx就是controllerThe provided functions are mainly provided by using it:给前端发送数据,Access plugin functionality
this.ctx.body="hello"====>It will only be executed once and then disconnected
可以再controllerCreate a new one in the folderjs文件,实现模块化
边栏推荐
猜你喜欢

Vulnhub target drone: HA_ NARAK

学习笔记14--机器学习在局部路径规划中的应用

Embedded Systems: Basic Timers

Chapter 12 贝叶斯网络

双向循环带头链表

php fails to write data to mysql

Redis缓存以及存在的问题--缓存穿透、缓存雪崩、缓存击穿及解决方法

七夕看什么电影好?爬取电影评分并存入csv文件

What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file

uniapp时间组件封装年-月-日-时-分-秒
随机推荐
程序设计中的感悟
Mysql 死锁和死锁的解决方案
P1160 队列安排
php向mysql写入数据失败
撕裂寂寞
微信 小程序 之PC端 不支持 wx.previewMedia 方法 故用自定义轮播图进行 模拟照片视频的播放
真正爱你的女人是这样的
VXE-Table融合多语言
软件系统测试和验收测试有什么联系与区别?专业软件测试方案推荐
Constellation ideal lover
iptables实现网络限制下ntp自定义端口同步时间
Stored procedure writing experience and optimization measures
Redis常用命令
每一个女孩曾经都是一个没有泪的天使
RedisTemplate: error template not initialized; call afterPropertiesSet() before using it
C语言制作-QQ聊天室
彩绘漂亮MM集
uniapp time component encapsulates year-month-day-hour-minute-second
作为一个男人必须明白的22个道理
字符串提取 中文、英文、数字