Skip to content

20-Node.js Modules - url.parse() is DEPRECATED #15

@bytesunite

Description

@bytesunite

Chapter 20 url.parse() is deprecated and needs to be updated to use WhatWg URL API.

https://nodejs.org/api/url.html#the-whatwg-url-api
https://url.spec.whatwg.org/

DEPRECATED url.parse() syntax:

  const url = require('url');
  const myUrl = url.parse("https://www.example.com/listing?id=1000&premium=true");
  console.log(myUrl); // the url object
  console.log(`Host: ${myUrl.host}`);  // www.example.com

WhatWg URL API syntax:

// taken from nodejs.org:
// The new URL constructor is accessible as a property of the global object. 
// It can also be imported from the built-in url module.
  const myUrl = new URL("https://www.example.com/listing?id=1000&premium=true");
  console.log(myUrl);  // the url object
  console.log(`Host: ${myUrl.host}`);  // www.example.com

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions