简体   繁体   中英

Parse URL without protocol in Node using constructor

How can I parse successfully www.stackoverflow.com as a URL using the WHATWG URL API in NodeJS?

If I try new URL("www.stackoverflow.com") I get the error error: URL constructor: www.stackoverflow.com is not a valid URL .

Note: url.parse is deprecated

Simply do this:

const parsedURL = new URL(url.startsWith("http") ? url : `https://${url}`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM