CODEMONDAY
Published in

CODEMONDAY

Node.js Alternative after url.parse and querystring Deprecated

Since url.parse and querystring.parse is deprecated here and here at least since last LTS version 14.

The doc doesn’t suggest alternatives, so I study and found the standard alternative here below:

First on URL

Legacy is url.parse

> url.parse('https://developer.mozilla.org/en-US/docs/Web/API/URL')Url {                                                                                                                     protocol: 'https:',                                                                                                     slashes: true,                                                                                                          auth: null,                                                                                                             host: 'developer.mozilla.org',                                                                                          port: null,                                                                                                             hostname: 'developer.mozilla.org',                                                                                      hash: null,                                                                                                             search: null,                                                                                                           query: null,                                                                                                            pathname: '/en-US/docs/Web/API/URL',                                                                                    path: '/en-US/docs/Web/API/URL',                                                                                        href: 'https://developer.mozilla.org/en-US/docs/Web/API/URL'                                                          }

Now use URL

> new URL('https://developer.mozilla.org/en-US/docs/Web/API/URL')URL {                                                                                                                     href: 'https://developer.mozilla.org/en-US/docs/Web/API/URL',                                                           origin: 'https://developer.mozilla.org',
protocol: 'https:',
username: '',
password: '',
host: 'developer.mozilla.org',
hostname: 'developer.mozilla.org',
port: '',
pathname: '/en-US/docs/Web/API/URL',
search: '',
searchParams: URLSearchParams {},
hash: ''
}

This one is the Web API standard. You can find the same in the browser as well. So, it’s better to comply with standard over Node own library.

Second on querystring

Legacy is querystring.parse

> query = querystring.parse('foo=bar&bar=baz')                                                                                                                                                        [Object: null prototype] { foo: 'bar', bar: 'baz' }                                                                                                                                                   > query.foo                                                                                                                                                                                           'bar'

Now use URLSearchParams

> new URLSearchParams('&foo=bar&bar=baz')                                                                                                                                                              URLSearchParams { 'foo' => 'bar', 'bar' => 'baz' }                                                                                                                                                    > foo = new URLSearchParams('&foo=bar&bar=baz')                                                                                                                                                        URLSearchParams { 'foo' => 'bar', 'bar' => 'baz' }                                                                                                                                                    > foo.get('bar')                                                                                                                                                                                      'baz'

Same reason here, you can also find URLSearchParams in the browser.

Hope this help. Cheers !

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store