query

Introduction

The query function is just a helper function to help extract query parameters from URLs. It uses the WHATWG URL API under the hood to extract the query parameters and convert it from a URLSearchParams to a JSON object.

If there are multiple query parameters with the same name, it will return an array instead of a string.

Usage

Simply call the query function inside of your handler.

const { createRoute, useQuery } = require('light');

const { route } = createRoute('query');

module.exports = route(async (req, res) => {
  const { id, name } = await useQuery(req.url);

  return {
    id,
    name,
  };
});

After starting the dev server, you can make a request to localhost:3000/?id=123&name=light and expect a response of

{
  "id": "123",
  "name": "light"
}

Note that by default all query parameters are strings, and you will need to cast them as necessary.

Last updated