Denoのランタイム環境のインストールと検証。
--allow-net
date-fns
ascii_table
オペレーティングシステムに適切なコマンドを使用してdenoをインストールしてください:Windows(PowerShell):
linux(ターミナル):
iwr https://deno.land/x/install/install.ps1 -useb | iex
インストールの確認:
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
最終出力はフォーマットされたテーブルです:
完全なコード(エラー処理とタイプ定義を備えた):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)
この強化された応答には、より詳細な説明とエラー処理が含まれ、より堅牢で初心者に優しいものになります。 プレースホルダーAPIキーを自分のものに置き換えることを忘れないでください。
以上がデノにコマンドラインの天気アプリを構築しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。