路由(Router)
框架约定了app/router.js文件用于统一所有路由规则。
Router 主要用来描述请求URL和具体承担执行动作的 Controller 的对应关系
定义路由(Router)
module.exports = app => {
const { router, controller } = app;
router.get('/user/:id', controller.user.info);
};
class UserController extends Controller {
async info() {
this.ctx.body = `hello ${ctx.params.id}`, 使用params接收
}
}
Router详细定义说明,完整路由定义包括5个主要部分
router.verb('path-match', app.controller.action);
router.verb('router-name', 'path-match', app.controller.action);
router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);
router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);
1. verb: 用户触发动作,get,post等所有HTTP方法
2. router-name: 给路由取一个别名,可以通过 Helper 提供的辅助函数 pathFor 和 urlFor 来生成 URL
3. path-match: 路由URL路径
4. middleware1: 在router里面可以配置多个中间件
5. controller: 指定路由映射到具体的controller上
注意事项
1. 在router定义中,可以支持多个middleware(中间件)
2. controller必须定义在app/controller目录中
3. 一个文件中可以包含多个Controller定义,定义路由的时候可以通过${fileName}.${functionName}的方式指定对应的controller
4. Controll支持子目录,可以通过${directoryName}.${fileName}.${functionName}的方式获取
// app/router.js
module.exports = app => {
const { router, controller } = app;
router.get('/home', controller.home);
router.get('/user/:id', controller.user.page); // 动态路由,可以接收参数id
router.post('/admin', isAdmin, controller.admin);
router.post('/user', isLoginUser, hasAdminPermission, controller.user.create);
router.post('/api/v1/comments', controller.v1.comments.create); // app/controller/v1/comments.js
};
参数获取
Query String方式
module.exports = app => {
app.router.get('/search', app.controller.search.index);
};
exports.index = async ctx => {
ctx.body = `searchInfo: ${ctx.query.name}`;
};
参数命名方式(动态路由)
module.exports = app => {
app.router.get('/user/:id/:name', app.controller.user.info);
};
exports.info = async ctx => {
ctx.body = `userInfo: ${ctx.params.id}, ${ctx.params.name}`;
};
表单内容的获取
module.exports = app => {
app.router.post('/form', app.controller.form.post);
};
exports.post = async ctx => {
ctx.body = `body: ${JSON.stringify(ctx.request.body)}`;
};
let options = {
url: '/form',
method: 'post',
data: {name: 'jack', age: 18},
headers: {'Content-Type': 'application/json'}
}
axios(options).then(data=> {console.log(data)})
exports.security = {
csrf: false
};
重定向
内部重定向
module.exports = app => {
app.router.get('/home/index', app.controller.home.index);
app.router.redirect('/', '/home/index', 302);
};
exports.index = async ctx => {
ctx.body = 'hello controller';
};
外部重定向
module.exports = app => {
app.router.get('/search', app.controller.search.index);
};
exports.index = async ctx => {
const type = this.ctx.request.query.type;
const q = this.ctx.request.query.q || 'node.js';
if (type === 'bing') {
this.ctx.redirect(`http://localhost:7001/home/index?q=${q}`);
} else {
this.ctx.redirect(`http://localhost:7001/user/123/jack?q=${q}`);
}
};
多路由映射
module.exports = app => {
require('./router/news')(app);
require('./router/admin')(app);
};
module.exports = app => {
app.router.get('/news/list', app.controller.news.list);
app.router.get('/news/detail', app.controller.news.detail);
};
module.exports = app => {
app.router.get('/admin/user', app.controller.admin.user);
app.router.get('/admin/log', app.controller.admin.log);
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!