Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse querystring parameter from URL in Fastify server?

I am a totally new to fastify but I have a fastify server running. I want to parse query string such as:

http://fake.com/?user=123&name=ali

I want to get "user" and "name" values from the URL above. My current code is like this:

fastify.route({
  method: 'GET',
  url: '/',
  handler: async (request, reply) => getCompanyUsers(request, reply, services)
});

I want to get values of "user" and "name" and then pass the values to getCompanyUsers function.

Any help is appreciated.

Thanks

like image 713
web-tasarimci Avatar asked Jul 31 '19 14:07

web-tasarimci


People also ask

How do you parse in Querystring?

parse() Method. The querystring. parse() method is used to parse a URL query string into an object that contains the key and pair values of the query URL.


2 Answers

You can access the querystring using request.query

You can look at the official documentation here https://github.com/fastify/fastify/blob/main/docs/Reference/Request.md

like image 151
Lineker Avatar answered Nov 14 '22 23:11

Lineker


 fastify.route({
  method: 'GET',
  url: '/',
 schema: {
   // request needs to have a querystring with a `name` parameter
   querystring: {
    name: { type: 'string' }
  }
   },
   handler: async (request, reply) => {
   // here you will get request.query if your schema validate
  }
})
like image 26
Mohammad Khalid Avatar answered Nov 15 '22 01:11

Mohammad Khalid