table of Contents
koa-router path parameter acquisition
The principle:
/: Id setting dynamic routing, upon user request, can obtain the dynamic path parameter
Dynamic parameters: ctx.params obtain dynamic routing Router.get (, function () {} 'id / product /')
Case:
/**
* Koa
* */
const Koa = require('koa');
const Router = require('koa-router')();
const App = new Koa();
Router.get('/',(ctx,next)=>{
ctx.body = 'Hello koa';
});
Router.get('/product/:id',(ctx)=>{
let ctxId = ctx.params;
ctx.body ={
ctxId
}
})
App.use(Router.routes());
App.use(Router.allowedMethods());
App.listen(3000,()=>{
console.log('quick start at port 3000');
});
Visit the following links: http: // localhost: 3000 / product / 88
Returns the result: { "ctxId": { "id": "88"}}