0
votes

[`const express = require('express'); const app = express(); const https = require('https');

const url = "https://api.thevirustracker.com/free-api?countryTimeline=US";

app.get("/", (req ,res) => { res.send("Server is Running")

https.get(url, (response) => {
    
    response.on("data", (data) => {

        const TimelineData = JSON.parse(data);
        console.log(TimelineData);
        
    })
})

})

app.listen(3000, ()=>console.log("Server is Running 0n 5000"));`]1

const express = require('express');
const app = express();
const https = require('https');

const url = "https://api.thevirustracker.com/free-api?countryTimeline=US";

app.get("/", (req ,res) => {
    res.send("Server is Running")

    https.get(url, (response) => {
        
        response.on("data", (data) => {

            const TimelineData = JSON.parse(data);
            console.log(TimelineData);
            
        })
    })
})



app.listen(3000, ()=>console.log("Server is Running 0n 5000"));
3
Welcome to StackOverflow! To get accurate answers, you should write what is your target and what problem is making you stuck, attach the portion of code interested (whole formatted and not only for certain parts) and also explain what you've tried to solve the issuesxKobalt
response.on("data", (data).... is not the whole body response. It can be fired multiple times and you have to concat all chunks together for your full response. "data" is a invalid json string because its sliced into multiple chunks. listen for the "end" event and do your json parsing there.Marc
Please provide what the JSON looks like if you want the correct answer.hacKaTun3s

3 Answers

1
votes

To deliver large data in an effective manner API send data in chunk/stream format. and to receive each chunk it triggers the 'data' event and in your case, it might be possible that API sends data in chunk format. and it will not send you complete data in a single event.

Let's assume the complete response of your API is : { name: 'bella', age: 34, count: 40138 }

And API send it in 2 chunks :

  • Chunk1: { name: 'bella', age: 34, count: 4013
  • Chunk2: 8 }

In that case Json.Parse() on Chunk1 or Chunk2 will not work and threw an exception.

To deal with this problem you need to listen to the 'end' event and capture data from the'data' and parse it in the 'end' event.

Use the below code:

const express = require('express');
const app = express();
const https = require('https');

const url = "https://archive.org/advancedsearch.php?q=subject:google+sheets&output=json";

app.get("/", (req, res) => {
  res.send("Server is Running")

  https.get(url, (response) => {
    var responseData = '';

    response.on("data", (dataChunk) => {
      responseData += dataChunk;

    })

    response.on('end', () => {
      const TimelineData = JSON.parse(responseData);
      console.log(TimelineData);
    });

  }).on('error', (e) => {
    console.error(e);
  });
})



app.listen(5000, () => console.log("Server is Running 0n 5000"));
0
votes

The "data" event can be fired multiple times: https://nodejs.org/api/http.html#http_class_http_clientrequest

You have to listen for the "end" event and concat all chunks from the "data" event togehter for the full body response.

const express = require('express');
const app = express();
const https = require('https');

const url = "https://api.thevirustracker.com/free-api?countryTimeline=US";

app.get("/", (req, res) => {

    res.send("Server is Running")

    https.get(url, (response) => {

        const chunks = [];

        response.on("data", (data) => {
            chunks.push(data);
        })

        response.on("end", () => {

            let size = chunks.reduce((prev, cur) => {
                return prev + cur.length;
            }, 0);

            let data = Buffer.concat(chunks, size).toString();

            console.log(JSON.parse(data))

        });

    })

})



app.listen(3000, () => console.log("Server is Running 0n 5000"));
0
votes

why are you using https? replace https with http and run it again.

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

const url = "https://api.thevirustracker.com/free-api?countryTimeline=US";

app.get("/", (req ,res) => {
    res.send("Server is Running")

    http.get(url, (response) => {
        
        response.on("data", (data) => {

            const TimelineData = JSON.parse(data);
            console.log(TimelineData);
            
        })
    })
})