Weather Data in express and node.js not printing in my command line

const { response } = require("response");
const express = require("express");
const https = require("https")
const app = express ();

app.get("/", function(req,res) {
  const url = "https://api.openweathermap.org/data/2.5/weather?q=London&applied=536bcef96b2f01cd9b9f076db90807fe&unit=metric";
  https.get(url, function(response) {
    console.log(response.statusCode);
  })

  response.on("data", function(data) {
    const weatherData = JSON.parse(data)
    console.log(weatherData);
  })

  res.send("Welcome to the future");
})

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

The problem here is that when I type response.on to get data from the url to print it in the command line, it brings const { response } = require ("express") as shown above which is very alien to me. Please, how do I fix it so I can get my weatherData printed in the CMD?


There are quite a few things you'll need to change.

First, this section is wrong:

https.get(url, function(response) {
    console.log(response.statusCode);
})
response.on("data", function(data) {
    const weatherData = JSON.parse(data)
    console.log(weatherData);
})

Since "response" is a parameter you receive from the callback on the "get" function, you need to declare the "response.on" inside the funcion scope, like this:

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

    response.on("data", function(data) {
        const weatherData = JSON.parse(data)
        console.log(weatherData);
    })
})

Also, the "data" event only delivers a chunk of data. You should be listening for an "end" event aswell, and only parse the data when you receive the "end" event

https.get(url, function(response) {
    console.log(response.statusCode);
    const result = []
    response.on("data", function(data) {
        result.push(data);
    })
    .on("end", function() {
        const weatherData = JSON.parse(result.join(""));
        console.log(weatherData);
    })
})

And since you're not using the module named "response", you also need to remove this:

const { response } = require("response");

And then correct all the typos that were already mentioned in the comments, which were:

  1. Add the missing quote " at require("express) on line 2
  2. Remove the extra backsting at console.log("listening on port 3000")`; on line 17
  3. Change the second query parameter on your URL on line 6 from "applied" to "appid"