I am new to backend programming and recently I've this problem that I cant receive any data from Open Map Weather API... I am trying to send my request with https/express in node.js... My API key and all the parameters are correct because everything went right in Postman... I would really appreciate it if somebody could help me with this... -Here is my code btw-
const exp = require("express");
const hhh = require("https");
const app = exp();
app.get("/", (req, res) => {
const url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=249e0887318ca2b591a7911fd54fe5fe";
hhh.get(url, (response) => {
console.log(response);
})
res.send("<h1>On Air 3000</h1>")
})
app.listen(3000, () => {
console.log("Listening on 3000");
})
From the official document [here] : https://nodejs.org/api/http.html#http_http_get_options_callback.
The callback is invoked with a single argument that is an instance of http.IncomingMessage.
So response
is an object of http.IncomingMessage class that allows you to get the response from API, it's not the result you see in the browser or Postman. You can see some code examples from the same document above.
For your case, you can check the code below, I tested and it worked :)
const exp = require("express");
const hhh = require("https");
const app = exp();
app.get("/", (req, res) => {
const url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=249e0887318ca2b591a7911fd54fe5fe";
hhh.get(url, (response) => {
var result = '';
//data come by chunks, we append new chunk to result
response.on('data', function (chunk) {
result += chunk;
});
//when we receive all the data, print to screen
response.on('end', function () {
console.log(result);
});
})
res.send("<h1>On Air 3000</h1>")
})
app.listen(3000, () => {
console.log("Listening on 3000");
})