Query Strings क्या हैं
Query string एक URL का वह हिस्सा है जो question mark के बाद आता है, key=value pairs hold करते हुए।
In this page:
Syntax
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
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. #}
आम गलतियां
- manually & पर split करना
- यह भूल जाना कि values strings हैं
- 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: