简体   繁体   中英

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

You can access the querystring using request.query

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM