app.js:
const express = require("express");
const https = require("https");
const app = express();
const port = 3000;
app.get("/",function(req,res){
const url ="https://maps.googleapis.com/maps/api/geocode/jsonaddress=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY;
console.log(response.statusCode);
response.on("data",function(data){
var jatin=JSON.parse(data);
console.log(jatin);
})
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Error on Console
app.listen(3000,function(){ console.log("server started on port 3000"); })
server started on port 3000 200 undefined:26 "long_name"
SyntaxError: Unexpected end of JSON input at JSON.parse () at IncomingMessage. (/home/jatin/Downloads/full_stack/Web-Development/maps/app.js:11:21) at IncomingMessage.emit (events.js:189:13) at IncomingMessage.Readable.read (_stream_readable.js:487:10) at flow (_stream_readable.js:931:34) at resume_ (_stream_readable.js:912:3) at process._tickCallback (internal/process/next_tick.js:63:19) [nodemon] app crashed - waiting for file changes before starting
The output is visible when I run it on browser but on the console it throws an error. For some reason JSON.parse() isn't working as expected.
I am trying to make a geocoding API call and in response, it gives me a JSON output... which when I enter it as a URL on the browser the expected output is received But when app.js is run on a node express server and when I hit my localhost:3000 I am getting the console error
Apparently the JSON.parse("data") is working but stops unexpectedly. Which leads to error.
JSON.parse
on it. Or use a library that supports parsing streams (e.g.fetch
) – Jonas Wilms