← Back to Node.js Course | Chapter 6: HTTP Module | Lesson 4 of 7

Query Strings क्या हैं

Query string एक URL का वह हिस्सा है जो question mark के बाद आता है, key=value pairs hold करते हुए।

In this page:

  1. Query Strings
Syntax
javascript
const url = new URL(req.url, 'http://localhost');
url.searchParams.get('name');

Query Strings

URL और URLSearchParams classes से इसे parse करें: new URL(req.url, base).searchParams.get("q")। Older querystring module भी काम करता है। Values हमेशा strings हैं और repeated keys getAll से पढ़ी जा सकती हैं।

Note: strings hand-splitting की बजाय URLSearchParams इस्तेमाल करें।

उदाहरण: Query strings

javascript
const url = new URL("http://localhost/search?q=node+js&page=2&tag=a&tag=b");
console.log(url.searchParams.get("q"));
console.log(Number(url.searchParams.get("page")) + 1);
console.log(url.searchParams.getAll("tag"));
console.log(Object.fromEntries(url.searchParams));

// Output:
// node js
// 3
// [ 'a', 'b' ]
// { q: 'node js', page: '2', tag: 'b' }

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. manually & पर split करना
  2. यह भूल जाना कि values strings हैं
  3. percent-encoded characters decode न करना
चैप्टर सारांश
  • URL और URLSearchParams इस्तेमाल करें
  • get एक string return करता है
  • getAll repeats handle करता है
  • Decoding automatic है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.