Javascript Koa 路由器:如何获取查询字符串参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43256916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:47:21  来源:igfitidea点击:

Koa router: How to get query string params?

javascriptnode.jsquery-stringkoakoa-router

提问by Alon

I'm using koa-router.

我正在使用 koa 路由器。

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 qsis undefined

qs未定义

Any help will be profoundly appreciated!

任何帮助将不胜感激!

回答by CodingWithSpike

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

根据文档,应该有一个ctx.request.query表示为对象的查询字符串项。

回答by Cory Robinson

You can use ctx.query(or long-hand ctx.request.query)

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

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

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