使用说明
星球信息API🪐
一个简单的REST API,提供有关太阳系行星的信息。
入门指南
先决条件
- Node.js(v14或更高版本)
- npm
安装
- 安装依赖项:
npm install
- 启动服务器:
npm start
对于自动重新加载的开发:
npm run dev
API将于 http://localhost:3000
API终点
基本URL
http://localhost:3000
可用端点
1.获取所有行星
GET /planets
返回太阳系中所有行星的列表。
查询参数:
sortBy-按字段排序(名称、与太阳的距离、直径等)order-排序顺序(asc、desc)
例子:
GET /planets?sortBy=distanceFromSun&order=asc
2.获取行星详细信息
GET /planets/{name}
返回特定行星的详细信息。
例子:
GET /planets/mars
3.获取行星天气
GET /planets/{name}/weather
返回行星的当前天气状况。
例子:
GET /planets/earth/weather
4.搜索行星
GET /planets/search
根据各种标准过滤行星。
查询参数:
habitable-按宜居性过滤(真/假)minMoons-最小卫星数maxDistance-与太阳的最大距离(百万公里)hasAtmosphere-氛围浓厚(真/假)
示例:
GET /planets/search?habitable=true
GET /planets/search?minMoons=1&maxDistance=500
GET /planets/search?hasAtmosphere=true
响应格式
所有回复均遵循以下结构:
{
"success": true,
"count": 8,
"data": [...]
}
Planet数据结构
每个行星物体都包含:
id-唯一标识符name-行星名称distanceFromSun-距离(单位:百万公里)diameter-直径(单位:千米)mass-质量为10^24 kgmoons-卫星数量orbitalPeriod-轨道周期(以地球日为单位)rotationPeriod-地球自转周期atmosphere-大气成分habitability-居住状态description-简要说明
示例用法
获取所有行星
curl http://localhost:3000/planets
获取火星详细信息
curl http://localhost:3000/planets/mars
获取地球天气
curl http://localhost:3000/planets/earth/weather
寻找宜居行星
curl http://localhost:3000/planets/search?habitable=true
发展
此API使用以下内容构建:
- Node.js
- Express.js
- CORS用于跨源请求
要添加新特征或修改行星数据,请编辑 server.js 文件。
许可证
麻省理工学院