KOA

5.KOA 路由

2020-01-06  本文已影响0人  帶頭二哥

路由

一、依靠 ctx.request.url 手动处理

// 引入 koa 模块
const Koa = require('koa')
// 创建 koa 应用
const app = new Koa()

app.use(async (ctx,next) => {
    if(ctx.request.path == '/home.html') {
        ctx.body = `Hello home.html`
    } else if(ctx.request.path == '/me.html') {
        ctx.body = `Hello me.html`
    }
})

// 启动应用
app.listen(3000)

二、使用 koa-router 中间件

安装

    # koa2 对应的版本是 7.x
    npm install --save koa-router

路由用法

基本用法
// 引入 koa 模块
const Koa = require('koa')
// 创建 koa 应用
const app = new Koa()

// 引入 koa-router 模块
const Router = require('koa-router')

// 1. 创建路由对象
const router = new Router()

// 2. 设置执行代码
router.get('/home',async (ctx,next) => {
    ctx.body = "home"
})

// 3. 注册路由
app.use(router.routes())
// 4. 允许所有路由方法
app.use(router.allowedMethods())

// 启动应用
app.listen(3000)
路由请求方式设置
router.get('/home',async (ctx,next) => {
    ctx.body = "get home"
})

router.post('/home',async (ctx,next) => {
    ctx.body = "post home"
})

router.put('/home', async (ctx,next) => {
    ctx.body = "put home"
})

router.del('/home', async (ctx,next) => {
    ctx.body = "del home"
})
router
.get('/home',async (ctx,next) => {
    ctx.body = "get home"
})
.post('/home',async (ctx,next) => {
    ctx.body = "post home"
})
.put('/home', async (ctx,next) => {
    ctx.body = "put home"
})
.del('/home', async (ctx,next) => {
    ctx.body = "del home"
})
// 等价于
/*
router.all('/home', async (ctx,next) => {
    ctx.body = "del home"
})
*/
通过正则表达式定义路由
router.get(/^\/t.*?t$/im,async (ctx,next) => {
    console.log("正则表达式被执行")
    ctx.body = "Hello World"
})
实现多路径路由
router.get(['/home','/user'],async (ctx,next) => {
    ctx.body = "Hello World"
})
实现多个中间件
router.get('/home',
async (ctx,next) => {
    console.log('middleware one')
    await next()
},
async (ctx,next) => {
    console.log('middleware two')
})
子路由或嵌套路由
// 1. 创建路由对象
const router = new Router()

// 2. 创建子路由
const homeRouter = new Router()
homeRouter.get('/home',async(ctx,next) => {
    ctx.body = "home"
})
router.use('/test1',homeRouter.routes(),homeRouter.allowedMethods())

const userRouter = new Router()
userRouter.get('/user', async (ctx,next) => {
    ctx.body = "user"
})
router.use('/test2',userRouter.routes(),userRouter.allowedMethods())

// 3. 注册路由
app.use(router.routes())
// 4. 允许所有路由方法
app.use(router.allowedMethods())

路由前缀
var router = new Router({
  prefix: '/users'
});
router.get('/home',async (ctx,next)=>{
    console.log(ctx.req.url)    // url: /users/home
})
路由重定向
router.get('/users',async (ctx,next) => {
    ctx.body = "Hello World"
})

router.get('/home', async (ctx,next) => {
    ctx.redirect('/users','abc');
})
为路由命名并通过跳转形式跳转到路由位置
router.get('h','/home/:id',async (ctx,next) => {
    ctx.body = 'Hello World' + ctx.params.id
})

console.log(router.url('h',2))
console.log(router.url('h',{id:2}))

// 通过获取 url 进行跳转
router.get('/user', async (ctx,next) => {
    ctx.redirect(router.url('h',{id:2}));
})

模块化路由

定义子路由模块

路由文件 /routes/users.js

"use strict";
// 创建路由对象
const router = require('koa-router')()

// 定义子路由处理
router
.get('/a',async (ctx,next) => {
    ctx.body = '/users => a'
})
.get('/b',async (ctx,next) => {
    ctx.body = '/users => b'
})

module.exports = router

路由文件 /routes/home.js

"use strict";
// 创建路由对象
const router = require('koa-router')()

// 定义子路由处理
router
.get('/a',async (ctx,next) => {
    ctx.body = '/users => a'
})
.get('/b',async (ctx,next) => {
    ctx.body = '/users => b'
})

module.exports = router
定义中央路由模块,统一配置路由

路由文件 /routes/index.js

"use strict";
/**
 * 中央路由模块,统一配置路由
 */
const router = require('koa-router')()

// 配置子路由
const homeRouter = require('./home.js')
router.use('/home',homeRouter.routes(),homeRouter.allowedMethods())

// 配置子路由
const userRouter = require('./user.js')
router.use('/user',userRouter.routes(),userRouter.allowedMethods())

module.exports = router
使用中央路由模块

app.js 中使用中央路由模块

const router = require('./routes')
app.use(router.routes())
上一篇 下一篇

猜你喜欢

热点阅读