0

This is the code

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


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

    const url ="http://api.openweathermap.org/geo/1.0/directq=London&unit=metric&appid=";
    https.get(url, function(response){
        console.log(response.statusCode);

        response.on("data", function(data) {
       let weatherData =  [];
       weatherData.push(data);
       const parse = JSON.parse(weatherData);
       console.log(parse);
         
        });
    });
    res.send("Server has started");
});





app.listen(3000, function() {
    console.log("Server running in port 3000");
}); 

When I try to console.log(data) in response.on() it gives me the hexadecimal value of of the url or api(hidden). I am trying to make a JS object of the data which is being the input of the callback function.

  • 1
    your data is returned in chunks - and that's all you get for the moment. You must combine all chunks to get the full object. see https://stackoverflow.com/questions/47986527/use-result-of-https-get-request-node-js – Plagiatus Mar 28 '23 at 12:07
  • May [this answer](https://stackoverflow.com/a/9578403/12715723) will help you. – Jordy Mar 28 '23 at 12:17

1 Answers1

0

While playing around i figured the solution.

Refer to the code below:

const express = require("express");
const app = express();
const http = require("http");

app.get("/", function(req, res) {
  const url = "http://api.openweathermap.org/data/2.5/weather?lat=23.3441&lon=85.3096&appid=a297d3b46d0b5a7aab6dde3512962b99";
  http.get(url, function(response){
    console.log(response.statusCode);
         
    response.on("data", function(data) {
      const weatherData = [];
      weatherData.push(data);
      const jdone = JSON.parse(weatherData);
      console.log(jdone);
    });  
  });
  res.send("Server has started");
});

app.listen(3000, function() {
  console.log("Server running in port 3000");
});