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