How to convert a query string to an object in Node.js

1 Answer

0 votes
const queryString = "?q=NodeJS&lang=en&limit=10";

let obj = new URLSearchParams(queryString);

console.log(obj.get('q')); 
console.log(obj.get('lang'));
console.log(obj.get('limit'));

obj = Object.fromEntries(new URLSearchParams(queryString));

console.log(obj);

  
  
  
  
/*
run:
  
NodeJS
en
10
{ q: 'NodeJS', lang: 'en', limit: '10' }

*/

 



answered Apr 22, 2022 by avibootz

Related questions

3 answers 257 views
3 answers 274 views
2 answers 260 views
2 answers 224 views
224 views asked Feb 27, 2020 by avibootz
1 answer 218 views
3 answers 377 views
1 answer 161 views
...