Konsep Utama:
--allow-net
date-fns
. ascii_table
Pemasangan:
Pasang Deno menggunakan arahan yang sesuai untuk sistem pengendalian anda:
Windows (PowerShell):
iwr https://deno.land/x/install/install.ps1 -useb | iex
linux (terminal):
curl -fsSL https://deno.land/x/install/install.sh | sh
macOS (homebrew):
brew install deno
deno --version
fail: index.ts
mkdir weather-app cd weather-app code index.ts // Or your preferred editor
mengambil input pengguna dan data API:
Nama bandar diluluskan sebagai hujah baris arahan. Kami akan menggunakan modul Deno untuk parsing argumen: flags
import { parse } from "https://deno.land/std@0.61.0/flags/mod.ts"; // ... (rest of the code)
mengambil data dari OpenWeatherMap: 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
Pemprosesan Data dan Pembentangan:
untuk output bersih: 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)
// ... (table creation and display) console.log(table.toString());
(nota: ganti dengan kunci API OpenWeatherMap sebenar anda.) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
// ... (imports as shown above) // ... (argument parsing and error handling as shown above) const apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; const res = await fetch( `https://api.openweathermap.org/data/2.5/forecast?q=${args.city}&units=metric&appid=${apiKey}`, ); if (!res.ok) { console.error(`Error fetching weather data: ${res.status} ${res.statusText}`); Deno.exit(1); } const data = await res.json(); // ... (Interface definition and data processing as shown in the previous sections) // ... (table creation and display as shown in the previous sections)
Atas ialah kandungan terperinci Bina aplikasi cuaca baris arahan di Deno. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!