As a beginner developer diving into the world of API's, I've encountered a recurring error in Node.js that seems to be causing crashes:
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (/Users/SA/Desktop/pokeApp/app.js:13:34)
at IncomingMessage.emit (events.js:314:20)
at IncomingMessage.Readable.read (_stream_readable.js:513:10)
at flow (_stream_readable.js:986:34)
at resume_ (_stream_readable.js:967:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
I'm unsure how to troubleshoot this issue or pinpoint the exact problem. Here is a snippet of my JavaScript code:
const express = require("express");
const https = require("https");
const app = express();
app.get("/", (req,res) => {
const url = "https://pokeapi.co/api/v2/pokemon/1/";
https.get(url, function(response){
console.log(response.statusCode);
response.on("data", (data) =>{
const pokemon = JSON.parse(data);
console.log(pokemon);
})
})
res.send("server running");
})
app.listen(3000, () => {
console.log("Port 3000");
})
This setup worked flawlessly with a weatherAPI, so I'm puzzled by this hiccup. I even ran the JSON through a lint tool and it came back clean.