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 258 views
3 answers 280 views
2 answers 261 views
2 answers 227 views
227 views asked Feb 27, 2020 by avibootz
1 answer 221 views
3 answers 379 views
1 answer 162 views
...