koa2 router koa-router路由配置

发布于 6 年前 作者 vueper 6417 次浏览 最后一次编辑是 6 年前 来自 分享

koa-router,也叫koa路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET、POST 等)组成的,涉及到应用如何响应客户端对某个网站节点的访问。

每一个路由都可以有一个或者多个处理器函数,当匹配到路由时,这个/些函数将被执行。

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

Koa-router以及Koa2入门实战教程下载: https://pan.baidu.com/s/1KNaA97kGwNhavch5rP_G7w

1、koa-router的基本配置

下面的代码展示了几个Koa2路由实例:

1、需要安装 koa-router

npm install koa-router --save

2、koa-router完整配置例子 引入 koa模块 以及koa-router模块 并实例化,然后配置路由启动路由

var Koa=require('koa');
var Router = require('koa-router');
//实例化
var app=new Koa();
var router = new Router();

//ctx  上下文 context ,包含了request 和response等信息

//配置路由
router.get('/',async (ctx)=>{

    ctx.body='首页';
})

router.get('/news',async (ctx)=>{

    ctx.body="这是一个新闻页面"
})
/*启动路由*/
app.use(router.routes())   
    .use(router.allowedMethods());   


app.listen(3000);


2、koa2 koa-router 动态路由配置

koa路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET、POST 等)组成的,涉及到应用如何响应客户端对某个网站节点的访问。 koa2中我们经常需要把某种模式匹配到的所有路由,全都映射到同个方法。例如,我们有一个 新闻详情的路由件,对于所有 ID 各不相同的用户,都要使用这个方法来渲染。那么,我们可以在 koa的路由路径中使用『动态路径参数/动态路由』

下面的代码展示了koa动态路由配置实例:

//引入 koa模块

var Koa=require('koa');
var router = require('koa-router')();  /*引入是实例化路由   推荐*/
//实例化
var app=new Koa();
router.get('/',async (ctx)=>{
    ctx.body="首页";
})
router.get('/news',async (ctx)=>{
    ctx.body="新闻列表页面";
})
//动态路由  http://localhost:3000/newscontent/xxxx
router.get('/newscontent/:aid',async (ctx)=>{

    //获取动态路由的传值
    console.log(ctx.params);  //{ aid: '456' }

    ctx.body="新闻详情";

})
//动态路由里面可以传入多个值

//http://localhost:3000/package/123/456

router.get('/package/:aid/:cid',async (ctx)=>{

    //获取动态路由的传值
    console.log(ctx.params);  //{ aid: '123', cid: '456' }
    ctx.body="新闻详情";
})
app.use(router.routes());   /*启动路由*/
app.use(router.allowedMethods());
app.listen(3000);

这个时候我们浏览器输入

http://localhost/newscontent/123

或者输入http://localhost/newscontent/1

或者输入http://localhost/newscontent/456

通过ctx.params就可以获取newscontent反斜杠后面的数据

3、koa2 koa-router路由get传值

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

下面是一个基本get传值的示例:


var Koa=require('koa');

var router = require('koa-router')();  /*引入是实例化路由** 推荐*/

//实例化
var app=new Koa();

router.get('/',async (ctx)=>{
    ctx.body="首页";

})

router.get('/news',async (ctx)=>{
    ctx.body="新闻列表页面";

})

//获取get传值
//http://localhost:3002/newscontent?aid=123

router.get('/newscontent',async (ctx)=>{

     //从ctx中读取get传值

    console.log(ctx.query);  //{ aid: '123' }       获取的是对象   用的最多的方式      ******推荐

    console.log(ctx.querystring);  //aid=123&name=zhangsan      获取的是一个字符串

    console.log(ctx.url);   //获取url地址

    //ctx里面的request里面获取get传值

    console.log(ctx.request.url);
    console.log(ctx.request.query);   //{ aid: '123', name: 'zhangsan' }  对象
    console.log(ctx.request.querystring);   //aid=123&name=zhangsan


    ctx.body="新闻详情";

})

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


4、koa koa-router路由层级 路由模块化

1、在 app 目录下创建名为 home.js 的文件,内容如下:

const Router = require('koa-router');
let router = new Router();
router.get('/',async(ctx)=>{
    ctx.body="这是前台首页";
})
router.get('/news',async(ctx)=>{
    ctx.body ='这是前台新闻页面';
})
router.get('/user',async(ctx)=>{
    ctx.body ='这是用户页面';
})
module.exports=router;

2、然后在主应用中加载子路由模块:

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

let home=require('./module/home.js');
let admin=require('./module/admin.js');
//装载所有子路由
let router = new Router();
router.use('/admin',admin.routes());
router.use('/',home.routes());

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

app.listen(3000,()=>{
    console.log('[demo] server is starting at port 3000');
});
1 回复
回到顶部