>本教程通过构建第一个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中文网其他相关文章!