Skip to content

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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