当前位置:网站首页>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文件,实现模块化
边栏推荐
猜你喜欢
随机推荐
Redis常用命令
爬虫之验证码
原型&原型链
Codeforce 8.1-8.7做题记录
Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
行走社会100绝招
moment的使用
路由----router
【结构体内功修炼】枚举和联合的奥秘(三)
busybox 知:构建
Redis implements distributed lock-principle-detailed explanation of the problem
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
Adb 授权过程分析
创业者如何吸引风险投资商
执子之手,与子偕老。你同意么?
宝塔实测-搭建中小型民宿酒店管理源码
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
Ethernet Principle
The magic weapon for small entrepreneurs!
SQL SERVER关于主从表触发器设计