>本教程通過構建第一個DENO應用程序來指導您:命令行天氣預報程序。 我們將介紹DENO安裝,通過OpenWeatherMap API獲取天氣數據,並在用戶友好的表中介紹預測。 強烈建議使用帶有DENO插件的Visual Studio代碼。 我們將使用Typescript來增強代碼清晰度。
>密鑰概念:
--allow-net
)。 date-fns
ascii_table
>使用適當的操作系統命令安裝DENO:
linux(終端):
iwr https://deno.land/x/install/install.ps1 -useb | iex
> macos(homebrew):
curl -fsSL https://deno.land/x/install/install.sh | sh
創建一個項目目錄和
>文件:brew install deno
deno --version
>
index.ts
城市名稱作為命令行的參數傳遞。 我們將使用deno的
mkdir weather-app cd weather-app code index.ts // Or your preferred editor
API從OpenWeatherMap檢索數據:
記住使用flags
>標誌運行:
import { parse } from "https://deno.land/std@0.61.0/flags/mod.ts"; // ... (rest of the code)
fetch
數據處理和表示:
// ... (API key and other code) const res = await fetch( `https://api.openweathermap.org/data/2.5/forecast?q=${args.city}&units=metric&appid=${apiKey}`, ); const data = await res.json(); // ... (error handling and data processing)
進行日期格式化,--allow-net
進行乾淨的輸出:
deno run --allow-net index.ts --city London
>完整的代碼(使用錯誤處理和類型定義):
(注意:用實際的OpenWeatherMap API鍵替換date-fns
>)
ascii_table
import { fromUnixTime, format } from "https://deno.land/x/date_fns@v2.15.0/index.js"; import AsciiTable from "https://deno.land/x/ascii_table/mod.ts"; // ... (data processing using interfaces and functions)
以上是在DeNo中構建命令行天氣應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!