Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 返回输入的意外结尾的JSON.parse()_Node.js_Api_Express - Fatal编程技术网

Node.js 返回输入的意外结尾的JSON.parse()

Node.js 返回输入的意外结尾的JSON.parse(),node.js,api,express,Node.js,Api,Express,[`const express=require('express'); 常量app=express(); 常量https=require('https') 常量url=”https://api.thevirustracker.com/free-api?countryTimeline=US“ 应用程序获取(“/”,(请求,请求)=>{ res.send(“服务器正在运行”) }) app.listen(3000,()=>console.log(“服务器正在运行0n5000”);`] const

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

常量url=”https://api.thevirustracker.com/free-api?countryTimeline=US“

应用程序获取(“/”,(请求,请求)=>{ res.send(“服务器正在运行”)

})

app.listen(3000,()=>console.log(“服务器正在运行0n5000”);`]

const express=require('express');
常量app=express();
常量https=require('https');
常量url=”https://api.thevirustracker.com/free-api?countryTimeline=US";
应用程序获取(“/”,(请求,请求)=>{
res.send(“服务器正在运行”)
https.get(url,(响应)=>{
响应.on(“数据”,(数据)=>{
const TimelineData=JSON.parse(数据);
console.log(TimelineData);
})
})
})
app.listen(3000,()=>console.log(“服务器正在运行0n5000”)可以多次触发“数据”事件:

您必须侦听“end”事件,并将“data”事件中的所有块连接到一起,以获得全身响应

const express=require('express');
常量app=express();
常量https=require('https');
常量url=”https://api.thevirustracker.com/free-api?countryTimeline=US";
应用程序获取(“/”,(请求,请求)=>{
res.send(“服务器正在运行”)
https.get(url,(响应)=>{
常量块=[];
响应.on(“数据”,(数据)=>{
推送(数据);
})
响应。在(“结束”、()=>{
让大小=块。减少((上一个,当前)=>{
返回上一个+当前长度;
}, 0);
让data=Buffer.concat(chunks,size).toString();
console.log(JSON.parse(数据))
});
})
})
app.listen(3000,()=>console.log(“服务器正在运行0n5000”);
为什么要使用https? 将https替换为http并再次运行

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);
            
        })
    })
})

为了以有效的方式交付大数据,API以块/流格式发送数据。为了接收每个区块,它会触发“数据””事件,在您的情况下,API可能会以区块格式发送数据。而且它不会在一次事件中向您发送完整的数据

假设API的完整响应为:
{姓名:“贝拉”,年龄:34岁,计数:40138}

API将其分为两部分发送:

  • Chunk1:
    {姓名:'bella',年龄:34岁,计数:4013
  • Chunk2:
    8}
在这种情况下,Chunk1或Chunk2上的Json.Parse()将不起作用,并引发异常

要解决此问题,您需要侦听“结束”事件,从“数据”中捕获数据,并在“结束”事件中解析数据

使用以下代码:

const express=require('express');
常量app=express();
常量https=require('https');
常量url=”https://archive.org/advancedsearch.php?q=subject:google+sheets&output=json”;
应用程序获取(“/”,(请求,请求)=>{
res.send(“服务器正在运行”)
https.get(url,(响应)=>{
var responseData=“”;
响应.on(“数据”,(数据块)=>{
responseData+=数据块;
})
响应。在('end',()=>{
const TimelineData=JSON.parse(responseData);
console.log(TimelineData);
});
}).on('错误',(e)=>{
控制台错误(e);
});
})

app.listen(5000,()=>console.log(“服务器正在运行0n5000”)欢迎来到StackOverflow!为了得到准确的答案,你应该写下你的目标和让你陷入困境的问题,附上感兴趣的代码部分(全部格式化,而不仅仅是某些部分),并解释你试图解决的问题
response.on(“数据”,(数据)..
不是全身响应。它可以被触发多次,您必须将所有块连接在一起才能获得完整响应。“数据”是无效的json字符串,因为它被分为多个块。请侦听“结束”事件并在那里进行json解析。
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);
            
        })
    })
})