You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
API(Application Programming Interface)是应用程序编程接口,是不同软件组件之间的通信约定。REST(Representational State Transfer)是一种基于 HTTP 协议的架构风格,用于设计网络应用程序的 API。
🌟 REST 核心原则
1. 无状态(Stateless)
每个请求包含所有必要信息
服务器不保存客户端状态
请求之间相互独立
2. 客户端-服务器架构
客户端负责用户界面
服务器负责数据存储
职责分离
3. 统一接口(Uniform Interface)
资源通过 URI 标识
通过 HTTP 方法操作资源
响应包含自描述数据
4. 可缓存(Cacheable)
响应可标记为可缓存
减少网络请求
提升性能
💻 HTTP 方法
方法
描述
幂等性
示例
GET
获取资源
是
GET /users
POST
创建资源
否
POST /users
PUT
更新资源(整体)
是
PUT /users/1
PATCH
更新资源(部分)
否
PATCH /users/1
DELETE
删除资源
是
DELETE /users/1
📡 RESTful API 示例
1. 基础结构
// GET /users - 获取用户列表fetch('/api/users').then(res=>res.json()).then(users=>console.log(users))// GET /users/1 - 获取特定用户fetch('/api/users/1').then(res=>res.json()).then(user=>console.log(user))