Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
add query guide
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed May 13, 2019
1 parent cf83cb8 commit bb5d15a
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion guides/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,40 @@ title: query
subtitle: use the query function to parse query parameters
---

## Coming Soon
## 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](https://nodejs.org/api/url.html#url_class_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 import the `query` function and pass it the `req` object.

```javascript
const { light, query } = require('light');

module.exports = light({
path: '/',

async handler(req) {
const { id, name } = query(req);

return {
id,
name,
};
},
});
```

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

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

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

0 comments on commit bb5d15a

Please sign in to comment.