const { response } = require("response");
const express = require("express");
const https = require("https")
const app = express ();
app.get("/", function(req,res) {
const url = "https://api.openweathermap.org/data/2.5/weather?q=London&applied=536bcef96b2f01cd9b9f076db90807fe&unit=metric";
https.get(url, function(response) {
console.log(response.statusCode);
})
response.on("data", function(data) {
const weatherData = JSON.parse(data)
console.log(weatherData);
})
res.send("Welcome to the future");
})
app.listen(3000, function() {
console.log("listening on port 3000");
})
這里的問題是,當我鍵入 response.on 以從 url 獲取資料以在命令列中列印它時,它會帶來如上所示的const { response } = require ("express"),這對我來說非常陌生。請問,我該如何修復它,以便我可以在 CMD 中列印我的 weatherData?
uj5u.com熱心網友回復:
有很多事情你需要改變。
首先,這部分是錯誤的:
https.get(url, function(response) {
console.log(response.statusCode);
})
response.on("data", function(data) {
const weatherData = JSON.parse(data)
console.log(weatherData);
})
由于“response”是您從“get”函式的回呼中收到的引數,因此您需要在函式范圍內宣告“response.on”,如下所示:
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", function(data) {
const weatherData = JSON.parse(data)
console.log(weatherData);
})
})
此外,“資料”事件僅傳遞一大塊資料。您還應該監聽“結束”事件,并且僅在收到“結束”事件時決議資料
https.get(url, function(response) {
console.log(response.statusCode);
const result = []
response.on("data", function(data) {
result.push(data);
})
.on("end", function() {
const weatherData = JSON.parse(result.join(""));
console.log(weatherData);
})
})
而且由于您沒有使用名為“response”的模塊,因此您還需要洗掉它:
const { response } = require("response");
然后更正評論中已經提到的所有錯別字,它們是:
- 在第 2 行的 require("express) 處添加缺少的引號 "
- 洗掉 console.log("listening on port 3000")` 處的額外支持;在第 17 行
- 將第 6 行 URL 上的第二個查詢引數從“applied”更改為“appid”
uj5u.com熱心網友回復:
首先確認您的網址有效
https://api.openweathermap.org/data/2.5/weather?q=London&appid=536bcef96b2f01cd9b9f076db90807fe&unit=metric
如果您使用的是 Windows 10 或 11,則不需要所有這些回應,只需在 cmd 行嘗試此操作(注意,您需要&url 中的每個都^像這樣轉義^&)
curl -o current.txt https://api.openweathermap.org/data/2.5/weather?q=London^&appid=536bcef96b2f01cd9b9f076db90807fe^&unit=metric
type current.txt
您可以將兩者都包含在一行中,但對于最后一個 & 不需要 ^escape 的 &type
curl -o current.txt https://api.openweathermap.org/data/2.5/weather?q=London^&appid=536bcef96b2f01cd9b9f076db90807fe^&unit=metric&type current.txt
下載后,您應該會在控制臺中看到回應。
因此您可以以任何方式呼叫該命令(隱藏或不隱藏),或者在螢屏上或您選擇的任何應用程式中讀取文本檔案。
當前.txt
{"coord":{"lon":-0.1257,"lat":51.5085},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":276.78,"feels_like":272.23,"temp_min":275.76,"temp_max":278.14,"pressure":999,"humidity":84},"visibility":10000,"wind":{"speed":6.17,"deg":250},"clouds":{"all":9},"dt":1641696366,"sys":{"type":2,"id":2019646,"country":"GB","sunrise":1641715415,"sunset":1641744666},"timezone":0,"id":2643743,"name":"London","cod":200}
uj5u.com熱心網友回復:
這是一個相當簡單的版本,使用該got()庫發出 http 請求。它是一個基于 Promise 的更高級別的庫,它比https在較低級別作業并且需要更多代碼才能正常作業和處理錯誤的庫更容易使用。
以下是您將如何使用got()庫執行此操作:
const got = require("got");
const express = require("express");
const app = express();
app.get("/", async function(req, res) {
try {
const url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=536bcef96b2f01cd9b9f076db90807fe&unit=metric";
const result = await got(url).json();
console.log(result);
res.json(result);
} catch (e) {
console.log(e);
res.sendStatus(500);
}
});
app.listen(3000, function() {
console.log("listening on port 3000");
});
變化:
- 固定網址(更改
applied為appid)。 - 切換到
got()http 請求的庫并內置 JSON 決議 - 添加錯誤處理
- 以 JSON 格式發送結果
這會生成以下輸出:
{
coord: { lon: -0.1257, lat: 51.5085 },
weather: [ { id: 800, main: 'Clear', description: 'clear sky', icon: '01n' } ],
base: 'stations',
main: {
temp: 276,
feels_like: 271.47,
temp_min: 274.33,
temp_max: 277.49,
pressure: 1000,
humidity: 86
},
visibility: 10000,
wind: { speed: 5.66, deg: 250 },
clouds: { all: 8 },
dt: 1641707384,
sys: {
type: 2,
id: 2019646,
country: 'GB',
sunrise: 1641715415,
sunset: 1641744666
},
timezone: 0,
id: 2643743,
name: 'London',
cod: 200
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409214.html
標籤:
