Categories
JavaScript Answers

How to fix Node js Error: Protocol “https:” not supported. Expected “http:”?

Spread the love

To fix Node js Error: Protocol "https:" not supported. Expected "http:", we use https.get method.

For instance, we write

const http = require("http");
const https = require("https");
const url = new URL("https://www.example.com");
const client = url.protocol == "https:" ? https : client;
const req = client.get(url, (res) => {
  console.log(res);
});

to get the client according to the url‘s protocol.

We use the https client if it’s https and http if it’s http.

Then we call get with the client for the right protocol to make the get request.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *