Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hey can you help me out . my 'url.parse' is deprecated

line 5 @deprecated — since v11.0.0 - Use the WHATWG URL API.

The signature '(urlStr: string): UrlWithStringQuery' of 'url.parse' is deprecated url .The declaration was marked as deprecated here.

like image 906
Ramzi Avatar asked Mar 22 '21 14:03

Ramzi


People also ask

Is parse deprecated?

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

Is URL deprecated?

If a URL is marked as deprecated, then it has been outdated by newer URLs. Deprecated URLs are supported for reasons of backward compatibility only. A deprecated URL may become obsolete in future versions of WebSphere Commerce.

What is URL parsing?

URL Parsing. The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.

Why do we parse URL?

URL parsing is a function of traffic management and load-balancing products that scan URLs to determine how to forward traffic across different links or into different servers. A URL includes a protocol identifier (http, for Web traffic) and a resource name, such as www.microsoft.com.


3 Answers

Here is the solution that worked for me.

const {URL} = require('url');

const getProfile = (req,res) => {
   const fullurl = req.protocol + '://' + req.get('host') + req.originalUrl;
   //E.g. http://127.0.0.1:3000/getProfile?pid=12345&section=NE
   
   const urlObj = new URL(fullurl);
   console.log(urlObj.searchParams.get('pid'));
   //prints 12345
   
   console.log(urlObj.searchParams.get('section'));
   //prints NE
}
like image 169
Pumpkin Pie Avatar answered Oct 16 '22 22:10

Pumpkin Pie


The new method is use WHATWG API in Node.Js
You can use the code below to get something like pathname:

const uri = 'https://yoururl.com/yourpathname'
const { URL } = require('url');
const yourUrl = new URL(uri);
const yourPathame = yourUrl.pathname.substr(1);

// yourUrl return 'https://yoururl.com/yourpathname'
// yourPathame return 'yourpathname'

The reference about WHATWG API is at Node.js website.

like image 25
William Mascarello Avatar answered Oct 17 '22 00:10

William Mascarello


Built-in url.parse was deprecated. The up to date api is URL. You can find more here: https://nodejs.org/api/url.html

like image 1
Denis Malykhin Avatar answered Oct 16 '22 23:10

Denis Malykhin