Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 706
Alon Avatar asked Apr 06 '17 13:04

Alon


People also ask

What are query string parameters in node JS?

The query parameter is the variable whose value is passed in the URL in the form of key-value pair at the end of the URL after a question mark (?). For example, www.geeksforgeeks.org? name=abc where, 'name' is the key of query parameter whose value is 'abc'.

What is Querystring JavaScript?

Query strings are used to retrieve information from the URL of pages, which can then be utilized for further functioning of the web application functionalities. Client-side also has its own set of helper methods and properties to help developers with that.


Video Answer


2 Answers

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

like image 190
CodingWithSpike Avatar answered Sep 18 '22 09:09

CodingWithSpike


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

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

like image 29
Cory Robinson Avatar answered Sep 19 '22 09:09

Cory Robinson