https api request is not working with https.get(url)

I need to pull the weather API data from

https://api.weatherapi.com/v1/current.json?key=f5f45b956fc64a2482370828211902&q=London

It gives a response when pasted in the web browser as well as in postman. But, once incorporated in javascript https.get it fails by continuously loading the browsers and hanging the terminal with unwanted informations.

app.js :

//jshint esversion:6

const express = require("express");
const https = require("https");

const app = express();


app.get("/",function(req, res){

  const url = "https://api.weatherapi.com/v1/current.json?key=f5f45b956fc64a2482370828211902&q=London";

  https.get(url,function(response){
    //console.log(response);

    response.on("data",function(data){
      const weatherData = JSON.parse(data);
      const temp = weatherData.current.temp_c;
      const weatherDescription = weatherData.current.condition.text;
    });
  });

  res.send("Server is up and running");

});


app.listen(3000, function(){
  console.log("Started on port 3000");
});

43 thoughts on “https api request is not working with https.get(url)”

Leave a Comment