2017-04-06 138 views
5

我正在使用koa-router。Koa路由器:如何獲取查詢字符串參數?

我怎樣才能得到請求的查詢字符串參數?

這是我好不容易纔寫的最好的:

import koaRouter from 'koa-router'; 

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

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

qs未定義

任何幫助將深刻地感謝!

回答

9

根據文檔應該有一個ctx.request.query這是查詢字符串項目表示爲一個對象。

+0

ctx.request.query未定義,雖然ctx.request.url是'/ api/courses?lecturer = John_Smith' – Alon

+0

@Alon你如何將路由器安裝到應用程序上? – robertklep

+0

我不知道發生了什麼變化,但突然ctx.request.query正在工作 – Alon

相關問題