【NodeJS】04koa 路由 get传值 动态路由

一、 Koa 路由

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

通俗的讲:路由就是根据不同的 URL 地址,加载不同的页面实现不同的功能

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

npm install --save koa-router
//引入 koa模块
const Koa = require('koa');
const router = require('koa-router')(); //注意:引入的方式

//实例化
const app = new Koa();
  router.get('/', function (ctx, next) {
  ctx.body="Hello koa";
})

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

//配置路由
router.get('/news,(ctx,next)=>{
ctx.body="新闻 page"
});

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

    ctx.body='首页'; /*返回数据 相当于:原生里面的res.writeHead() res.end()*/
}).get('/news',async (ctx)=>{

    ctx.body="这是一个新闻页面"
})

app.use(router.routes()); //作用:启动路由
app.use(router.allowedMethods()); // 作用: 这是官方文档的推荐用法,我们可以
看到 router.allowedMethods()用在了路由匹配 router.routes()之后,所以在当所有
路由中间件最后调用.此时根据 ctx.status 设置 response 响应头

app.listen(3000,()=>{
console.log('starting at port 3000');
});

优化:新建一个路由文件,专门存放路由信息

//引入 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="新闻列表页面";
})

router.get('/newscontent',async (ctx)=>{
    ctx.body="新闻详情";
})

app.use(router.routes());   /*启动路由*/
app.use(router.allowedMethods());

/*
*  router.allowedMethods()作用: 这是官方文档的推荐用法,我们可以
 看到 router.allowedMethods()用在了路由匹配 router.routes()之后,所以在当所有
 路由中间件最后调用.此时根据 ctx.status 设置 response 响应头*/
app.listen(3002);

二、 Koa 路由 get 传值

在 koa2 中 GET 传值通过 request 接收,但是接收的方法有两种:queryquerystring

query:返回的是格式化好的参数对象。
querystring:返回的是请求字符串。

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

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

router.get('/', function (ctx, next) {
  ctx.body="Hello koa";
})

router.get('/newscontent,(ctx,next)=>{
	let url =ctx.url;
	//从 request 中获取 GET 请求
	let request =ctx.request;
	let req_query = request.query;
	let req_querystring = request.querystring;
	//从上下文中直接获取
	let ctx_query = ctx.query;
	let ctx_querystring = ctx.querystring;
	ctx.body={
		url,
		req_query,
		req_querystring,
		ctx_query,
		ctx_querystring
	}
});
app.use(router.routes()); //作用:启动路由
app.use(router.allowedMethods()); //作用: 当请求出错时的处理逻辑
app.listen(3000,()=>{
	console.log('starting at port 3000');
});

优化

//引入 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="新闻列表页面";
})
//获取get传值
//http://localhost:3002/newscontent?aid=123
router.get('/newscontent',async (ctx)=>{
    /*在 koa2 中 GET 传值通过 request 接收,
    但是接收的方法有两种:query 和 querystring。
     query:返回的是格式化好的参数对象。
     querystring:返回的是请求字符串。*/
     
    //从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());
/*
*  router.allowedMethods()作用: 这是官方文档的推荐用法,我们可以
 看到 router.allowedMethods()用在了路由匹配 router.routes()之后,所以在当所有
 路由中间件最后调用.此时根据 ctx.status 设置 response 响应头*/
app.listen(3002);

三、 Koa 动态路由

//请求方式 http://域名/product/123
router.get('/product/:aid',async (ctx)=>{
	console.log(ctx.params); //{ aid: '123' } //获取动态路由的数据
	ctx.body='这是商品页面';
});

在app.js中使用

//引入 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:3002/newscontent/xxxx
router.get('/newscontent/:aid',async (ctx)=>{

    //获取动态路由的传值
    console.log(ctx.params);  //{ aid: '456' }
    ctx.body="新闻详情";

})
//动态路由里面可以传入多个值
//http://localhost:3002/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);
发布了145 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42554191/article/details/104010457