(一)GET方法 - 使用两种方式获取path
1.ctx.request
(1)直接取到键值对形式
ctx.request.query
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
let request = ctx.request;
let req_query = request.query;
ctx.body = {
req_query,
}
})
app.listen(3000, ()=> {console.log('[demo2] server is starting ...')})
(2)字符串形式
ctx.request.querystring
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
let request = ctx.request;
let req_querystring = request.querystring;
ctx.body = {
req_querystring
}
})
app.listen(3000, ()=> {console.log('[demo2] server is starting ...')})
2.ctx.query
(1)直接获得键值对形式
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
let ctx_query = ctx.query;
ctx.body = {
ctx_query,
}
})
app.listen(3000, ()=> {console.log('[demo2] server is starting ...')})
(2)获取字符串形式
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
let ctx_querystring = ctx.querystring;
ctx.body = {
ctx_querystring ,
}
})