Koa 路由

本文介绍了Koa中的路由配置方法,包括基本路由设置、GET请求参数处理及动态路由使用技巧。

1. Koa 路由

路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET、POST 等) 组成的,涉及到应用如何响应客户端对某个网站节点的访问。
通俗的讲:路由就是根据不同的 URL 地址,加载不同的页面实现不同的功能。

Koa 中的路由和 Express 有所不同,在 Express 中直接引入 Express 就可以配置路由,但是在 Koa 中我们需要安装对应的 koa-router 路由模块来实现。

npm install koa-router --save
const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

router.get('/',async (ctx) => {
  ctx.body = "hello koa"
})

router.get('/news',async (ctx) => {
  ctx.type = 'html';
  ctx.body = '<h2>新闻列表</h2>';
})

// 调用router.routes()来组装匹配好的路由,返回一个合并好的中间件
app.use(router.routes());
/*
  调用router.allowedMethods()获得一个中间件,当发送了不符合的请求时,
  会返回 `405 Method Not Allowed` 或 `501 Not Implemented`
*/ 
app.use(router.allowedMethods({ 
    // throw: true, // 抛出错误,代替设置响应头状态
    // notImplemented: () => '不支持当前请求所需要的功能',
    // methodNotAllowed: () => '不支持的请求方式'
}));

app.listen(3000,()=>{ 
  console.log('应用已经启动,http://localhost:3000'); 
});

2. Koa 路由 get 传值

在 koa2 中 GET 传值通过 request 接收,但是接收的方法有两种:query 和 querystring。
query:返回的是格式化好的参数对象。
querystring:返回的是请求字符串。

const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

// http://localhost:3000/newsContent?id=123&name=sport
router.get('/newsContent',async (ctx) => {
  /**
   * 从ctx中读取get传值
   */
  console.log(ctx.query);  // { id: '123', name: 'sport' }  推荐
  console.log(ctx.querystring); // id=123&name=sport 
  console.log(ctx.url); // /newsContent?id=123&name=sport
  /**
   * 从ctx中读取get传值
   */
  console.log(ctx.request.query);  // { id: '123', name: 'sport' }
  console.log(ctx.request.querystring); // id=123&name=sport 
  console.log(ctx.request.url); // /newsContent?id=123&name=sport

  ctx.body="新闻详情"
})


app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000,()=>{ 
  console.log('应用已经启动,http://localhost:3000'); 
});

3. Koa 动态路由

const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

// http://localhost:3000/newsContent/123
router.get('/newsContent/:id',async (ctx) => {
  // 获取动态路由传入的值
  console.log(ctx.params);  // { id: '123' }
  ctx.body="新闻详情"
})

/*
动态路由里可传入多个值
*/
// http://localhost:3000/newsDetail/123/456
router.get('/newsDetail/:id/:aid',async (ctx) => {
  console.log(ctx.params);  // { id: '123', aid: '456' }
  ctx.body="新闻具体"
})

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000,()=>{ 
  console.log('应用已经启动,http://localhost:3000'); 
});
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明天也要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值