DEV Community

Paweł Partyka
Paweł Partyka

Posted on

Query parameters in node.js

Today we will use Cavia to parse the request query parameters.

Step 1: Installation

First, let's install the @caviajs/http-query package.

npm install @caviajs/http-query --save
Enter fullscreen mode Exit fullscreen mode

Step 2: Usage

Now, use the HttpQuery in route handler to parse query parameters.

import { Route } from '@caviajs/http-router';
import { HttpQuery } from '@caviajs/http-query'; // 👈

export const GuineaPigListRoute: Route = {
  handler: async (request, response): Promise<void> => {
    const query = HttpQuery.parse(request); // 👈

    console.log(query); // 👈 do sth with query parameters...
  },
  method: 'GET',
  path: '/guinea-pigs/list',
};
Enter fullscreen mode Exit fullscreen mode

Remember to add the newly created route to the HttpRouter instance and start the server.

Step 3: Make request

Make a request to newly created endpoint.

curl -XGET 'http://localhost:8080/guinea-pigs/list?name=foo'
Enter fullscreen mode Exit fullscreen mode

Yes, it's that simple.
See you!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay