简体   繁体   中英

Koa router: How to get query string params?

I'm using koa-router.

How can I get the request's query string params?

This is the best I managed to write:

import koaRouter from 'koa-router';

const router = koaRouter({ prefix: '/courses' });

router.get('/', async (ctx) => {
        console.log(ctx.qs["lecturer"]);
    });

but qs is undefined

Any help will be profoundly appreciated!

According to the docs there should be a ctx.request.query that is the query string items represented as an object.

An update has changed this...

//URL parameters
//Named route parameters are captured and added to ctx.params.

router.get('/:category/:title', (ctx, next) => {
  console.log(ctx.params);
  // => { category: 'programming', title: 'how-to-node' }
});

你可以使用ctx.query (或长手ctx.request.query

app.use( (ctx) => console.log(ctx.query) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM