簡體   English   中英

Koa路由器:如何獲取查詢字符串參數?

[英]Koa router: How to get query string params?

我正在使用koa-router。

如何獲取請求的查詢字符串參數?

這是我設法寫的最好的:

import koaRouter from 'koa-router';

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

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

但是qs是未定義的

任何幫助將深表感謝!

根據文檔,應該有一個ctx.request.query ,它是表示為對象的查詢字符串項。

更新改變了這個......

//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) )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM