axios 源码解读
What is Axios?
Axios ia a promise-based HTTP Client for node.js and the browser.
Axios 是一个 HTTP Client,用于 node.js 和 浏览器。
it is isomorphic(同构的)(=it can run in the browser and node.js with the same codebase).
On the server-side it uses the native node.js http module, while on the client it uses XMLHTTPRequests.
Features
- Make XMLHttpRequests from the browser 从浏览器端发送 XMLHttpRequest 请求
- Make http requests from node.js 从 node.js 端发送 http 请求
- Supports the Promise API 支持 Promise API
- Intercept request and response 拦截请求和响应
- Transform request and response data 转换请求和响应数据
- Cancel requests
- Timeouts
- Query parameters serialization with support for nested(嵌套) entries 支持嵌套的查询参数序列化嵌套条目
- Automatic request body serialization to: 自动将请求体序列化为:
- json( application/json )
- Multipart / FormData( multiple/form-data )
- URL encoded form( application/x-www-form-urlencoded )
- Posting HTML forms as JSON 以 JSON 形式发布 HTML 表单
- Automatic JSON data handling in response 响应时自动处理 JSON 数据
- Progess capturing for browser and node.js with extra info(speed rate, remaining time) 携带额外信息的浏览器和 node.js 的进度捕获。
- Setting bandwidth limits for node.js 设置 node.js 带宽限制。
- Compatible with spec-compliant FormData and Blob (including node.js) 兼容数据规范和 Blob
- Client side support for protecting against XSRF 客户端支持防止XSRF
1 | ├── /dist/ # 项目输出目录 |
graph TD a["index.js/require('./lib/axios')"] --> b["createInstance(defaults)"] b --> c["new Axios(defaults)"] c --> d["Constructor Axios"]