美文网首页
koa路由使用

koa路由使用

作者: 夏夏夏夏顿天 | 来源:发表于2018-11-08 13:22 被阅读20次

其实,router这个是一个很重要的内容,用户进行url访问就可以更好的使用

不用router中间件

app.use(async (ctx,next)=>{
  if (ctx.path === '/') {
    ctx.body = 'we are at home!';
  } else {
    await next;
  }
})

app.use(async (ctx,next)=>{
  if (ctx.path === '/404') {
    ctx.body = 'page not found';
  } else {
    await next;
  }
})

中间件写法

const Koa = require('koa')
const fs = require('fs')
const app = new Koa()

const Router = require('koa-router')

let home = new Router()

// 子路由1
home.get('/', async ( ctx )=>{
  let html = `
    <ul>
      <li><a href="/page/helloworld">/page/helloworld</a></li>
      <li><a href="/page/404">/page/404</a></li>
    </ul>
  `
  ctx.body = html
})

// 子路由2
let page = new Router()
page.get('/404', async ( ctx )=>{
  ctx.body = '404 page!'
}).get('/helloworld', async ( ctx )=>{
  ctx.body = 'helloworld page!'
})

// 装载所有子路由
let router = new Router()
router.use('/', home.routes(), home.allowedMethods())
router.use('/page', page.routes(), page.allowedMethods())

// 加载路由中间件
app.use(router.routes()).use(router.allowedMethods())

app.listen(3000, () => {
  console.log('[demo] route-use-middleware is starting at port 3000')
})

koa-router相关API

router
  .get('/', async (ctx, next) => {
    ctx.body = 'Hello World!';
  })
  .post('/users', async (ctx, next) => {...})
  .put('/users/:id', async (ctx, next) => {...})
  .del('/users/:id', async (ctx, next) => {...})
  .all('/users/:id', async (ctx, next) => {...})

router.all()用于表示上述所有的动词方法

router.get('/', async (ctx,next) => {
  ctx.body = 'Hello World!';
});

上面代码中,router.get方法的第一个参数是根路径,第二个参数是对应的函数方法。

路由参数

我们可以通过ctx.params得到URL参数

router.get('/:category/:title', function (ctx, next) {
  console.log(ctx.params);
  // => { category: 'programming', title: 'how-to-node' }
});

这里有个中路由中间件汇集https://cnodejs.org/topic/57838dfaee5f048d54f90877

参考:

https://chenshenhai.github.io/koa2-note/note/route/koa-router.html

https://gaianote.github.io/2017/04/24/koa2%E5%AD%A6%E4%B9%A0%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B3_%E8%B7%AF%E7%94%B1%E4%B8%8Ekoa-router/

https://github.com/minghe/koa-book/blob/master/start/router.md

相关文章

  • Koa-router路由插件

    koa-router插件:koa路由插件,koa强依赖与此。 koa-router插件,使用流程: r1.get(...

  • 上传图片需求

    koa-body 的基本使用 在 koa2 中使用 koa-body,我使用的是全局引入,而不是路由级别的引入,因...

  • koa-router模块用法

    koa-router模块 koa必不可少的路由处理中间件,之前使用过koa-route模块,用法和koa-rout...

  • koa2.x学习3

    Koa 应用生成器以及 Koa 路由模块化 Koa 应用生成器Koa 搭建模块化路由/层级路由 Koa 应用生成器...

  • koa路由使用

    其实,router这个是一个很重要的内容,用户进行url访问就可以更好的使用 不用router中间件 中间件写法 ...

  • 使用@route装饰器简化koa-router写法

    前言 本文所有代码见github koa-router koa-router是koa项目中被广泛使用到的一个路由中...

  • koa2 用到的中间件

    koa-router //koa路由 koa-bodyparser //post 获取值 koa-stat...

  • 5.KOA 路由

    路由 一、依靠 ctx.request.url 手动处理 二、使用 koa-router 中间件 安装 路由用法 ...

  • koa-router模块

    koa必不可少的路由处理中间件,之前使用过koa-route模块,用法和koa-router大致一样,但是没有ko...

  • koa模块总结

    第三方模块 1. koa-router 作用:监听路由,编写接口时使用安装:npm i -S koa-router...

网友评论

      本文标题:koa路由使用

      本文链接:https://www.haomeiwen.com/subject/kxvixqtx.html