⚡
light
  • light
  • guides
    • getting started
    • server vs serverless
    • routes
    • models
    • factories
    • routing
    • plugins
    • middleware
    • query
    • params
    • global
    • error handling
    • hot module reloading (TODO)
    • databases (TODO)
      • mongoose
      • objection/knex
    • testing (TODO)
      • ava
      • jest
    • deployments (TODO)
      • runkit
      • aws
      • now
      • heroku
      • server
      • google cloud
      • azure
      • netlify
    • misc (TODO)
      • apollo/graphql
  • api
    • server({ routes, opts }): object
    • params(path, url): Promise<object>
    • boom
      • Coming soon
    • micro
      • Coming soon
      • Coming soon
      • Coming soon
      • Coming soon
      • Coming soon
      • Coming soon
Powered by GitBook
On this page
  • Introduction
  • Usage

Was this helpful?

  1. guides

query

PreviousmiddlewareNextparams

Last updated 5 years ago

Was this helpful?

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 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,
  };
});
{
  "id": "123",
  "name": "light"
}

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

After starting the dev server, you can make a request to and expect a response of

URLSearchParams
localhost:3000/?id=123&name=light