Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update how-to-access-query-string-parameters.md #2148

Merged
5 commits merged into from
Mar 14, 2019

Conversation

Yash-Handa
Copy link
Contributor

Hey @chowdhurian and @fhemberger,
In accordance with issue #1977, I have updated the Article how-to-access-query-string-parameters with the following changes:

  • Added a note on how to execute the program.
  • Added some detail about other key values provided by url.parse().
  • some other miscellaneous updates and refactors.

If there are any suggestion/enhancement/tips please feel free to tell me, I would love to incorporate those changes in this Article 😄


The `url.parser()` method returns an object which have many key value pairs one of which is the `query` object. Some other handy information returned by the method include `host`, `pathname`, `search` keys.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be url.parse instead of parser

@jonchurch
Copy link
Contributor

jonchurch commented Mar 11, 2019

I was working on this a moment ago hehe, I had added a section mentioning the builtin querystring module, explaining the differences between parsing with url and querystring.

Parsing with querystring

Another way to access query string parameters is parsing them using the querystring builtin Node module.

This method, however, must be passed just a querystring portion of a url. Passing it a whole url, like we did in the url.parse example, won't properly parse the querystrings.

const querystring = require('querystring')
const qs = "code=string&key=12&id=false"
const url = "http://example.com/index.html?code=string&key=12&id=false"

console.log(querystring.parse(qs))
// > { code: 'string', key: '12', id: 'false' }

console.log(querystring.parse(url))
// > { 'http://example.com/index.html?code': 'string', key: '12', id: 'false' }

I also edited the initial example, since the HTTP server wasn't specific to the example.

Parsing with url

In Node.js, functionality to aid in the accessing of URL query string parameters is built into the standard library. The built-in url.parse method takes care of most of the heavy lifting for us.

You can parse a url string into an object, including the querystrings, by passing a url to the built in url module's parse method.

To parse the querystrings themselves into an object, you must pass true as the second parameter to url.parse. Without passing true, the query value of the url object will just a single string.

Once parsed, we can access the query string parameters from the query property on the returned url object.

const url = require("url");
const urlString = "http://example.com/index.html?code=string&key=12&id=false";

const urlObject = url.parse(urlString, true);
const queryObject = urlObject.query;

console.log(queryObject);
// > { code: 'string', key: '12', id: 'false' }

@Yash-Handa
Copy link
Contributor Author

Thanks, @jonchurch, for such thoughtful content😄
I have added the querystring part to the Article. I would suggest keeping the HTTP server creation part in the Article because it serves as a complete and practical example.

If you further like to update other Articles in the Knowlagebase I would suggest:

  1. Select 4-5 articles to start with (or any number of articles you comfortable with).
  2. Put the list of selected Articles on issue Review needed for knowledge base articles #1977 with some summary like in here.
  3. And get started 🎉🎉

We would love to have your contributions.🙂

@ghost
Copy link

ghost commented Mar 13, 2019

@jonchurch:Please have a review again if possible, there're changes according to your suggestions :)

Copy link
Contributor

@keywordnew keywordnew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ghost ghost merged commit 378ab09 into nodejs:master Mar 14, 2019
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants