AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

Axios Node 端請求是如何實現(xiàn)的?

來源: 責編: 時間:2024-06-05 17:41:33 126觀看
導讀本文我們將討論 axios 的 Node 環(huán)境實現(xiàn)。我們都知道使用 axios 可以讓我們在瀏覽器和 Node 端獲得一致的使用體驗。這部分是通過適配器模式來實現(xiàn)的。axios 內(nèi)置了 2 個適配器(截止到 v1.6.8 版本)[8]:xhr.js 和 http.j
本文我們將討論 axios 的 Node 環(huán)境實現(xiàn)。我們都知道使用 axios 可以讓我們在瀏覽器和 Node 端獲得一致的使用體驗。這部分是通過適配器模式來實現(xiàn)的。

axios 內(nèi)置了 2 個適配器(截止到 v1.6.8 版本)[8]:xhr.js 和 http.js。86q28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片86q28資訊網(wǎng)——每日最新資訊28at.com

顧名思義,xhr.js 是針對瀏覽器環(huán)境提供的 XMLHttpRequest 封裝的;http.js 則是針對 Node 端的 http/https 模塊進行封裝的。86q28資訊網(wǎng)——每日最新資訊28at.com

不久前,我們詳細講解了瀏覽器端的實現(xiàn),本文就來看看 Node 環(huán)境又是如何實現(xiàn)的。86q28資訊網(wǎng)——每日最新資訊28at.com

Node 端請求案例

老規(guī)矩,在介紹實現(xiàn)之前,先看看 axios 在瀏覽器器環(huán)境的使用。86q28資訊網(wǎng)——每日最新資訊28at.com

首先創(chuàng)建項目,安裝 axios 依賴:86q28資訊網(wǎng)——每日最新資訊28at.com

mdir axios-demoscd axios-demosnpm initnpm install axios# 使用 VS Code 打開當前目錄code .

寫一個測試文件 index.js:86q28資訊網(wǎng)——每日最新資訊28at.com

// index.jsconst axios = require('axios')axios.get('https://httpstat.us/200')  .then(res => {    console.log('res >>>>', res)  })

執(zhí)行文件:86q28資訊網(wǎng)——每日最新資訊28at.com

node --watch index.js

注意:--watch[9] 是 Node.js 在 v16.19.0 版本引入的實驗特性,在 v22.0.0 已轉(zhuǎn)為正式特性。86q28資訊網(wǎng)——每日最新資訊28at.com

打印出來結(jié)果類似:86q28資訊網(wǎng)——每日最新資訊28at.com

Restarting 'index.js'res >>>> {  status: 200,  statusText: 'OK'  headers: Object [AxiosHeaders] {}  config: {}  request: <ref *1> ClientRequest {}  data: { code: 200, description: 'OK' }}Completed running 'index.js'

修改 Index.js 文件內(nèi)容保存:86q28資訊網(wǎng)——每日最新資訊28at.com

const axios = require('axios')axios.get('https://httpstat.us/404')  .catch(err => {    console.log('err >>>>', err)  })

打印結(jié)果類似:86q28資訊網(wǎng)——每日最新資訊28at.com

Restarting 'index.js'err >>>> AxiosError: Request failed with status code 404 {  code: 'ERR_BAD_REQUEST',  config: {}  request: <ref *1> ClientRequest {}  response: {    status: 404,    statusText: 'Not Found',    data: { code: 404, description: 'Not Found' }  }}

以上我們就算講完了 axios 在 Node 端的簡單使用,這就是 axios 好處所在,統(tǒng)一的使用體驗,免去了我們在跨平臺的學習成本,提升了開發(fā)體驗。86q28資訊網(wǎng)——每日最新資訊28at.com

源碼分析

接下來就來看看 axios 的 Node 端實現(xiàn)。源代碼位于 lib/adapters/http.js[10] 下。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L160export default isHttpAdapterSupported && function httpAdapter(config) {/* ... */}

Node 端發(fā)出的請求最終都是交由 httpAdapter(config) 函數(shù)處理的,其核心實現(xiàn)如下:86q28資訊網(wǎng)——每日最新資訊28at.com

import http from 'http';import https from 'https';export default isHttpAdapterSupported && function httpAdapter(config) {  // 1)  return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {        // 2)    let {data, lookup, family} = config;    const {responseType, responseEncoding} = config;    const method = config.method.toUpperCase();        // Parse url    const fullPath = buildFullPath(config.baseURL, config.url);    const parsed = new URL(fullPath, 'http://localhost');        const headers = AxiosHeaders.from(config.headers).normalize();        if (data && !utils.isStream(data)) {      if (Buffer.isBuffer(data)) {        // Nothing to do...      } else if (utils.isArrayBuffer(data)) {        data = Buffer.from(new Uint8Array(data));      } else if (utils.isString(data)) {        data = Buffer.from(data, 'utf-8');      } else {        return reject(new AxiosError(          'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',          AxiosError.ERR_BAD_REQUEST,          config        ));      }    }        const options = {      path,      method: method,      headers: headers.toJSON(),      agents: { http: config.httpAgent, https: config.httpsAgent },      auth,      protocol,      family,      beforeRedirect: dispatchBeforeRedirect,      beforeRedirects: {}    };        // 3)      let transport;    const isHttpsRequest = /https:?/.test(options.protocol);        if (config.maxRedirects === 0) {      transport = isHttpsRequest ? https : http;    }        // Create the request    req = transport.request(options, function handleResponse(res) {      // ...    }        // 4)    // Handle errors    req.on('error', function handleRequestError(err) {      // @todo remove      // if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;      reject(AxiosError.from(err, null, config, req));    });        // 5)    // Handle request timeout    if (config.timeout) {      req.setTimeout(timeout, function handleRequestTimeout() {        if (isDone) return;        let timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';        const transitional = config.transitional || transitionalDefaults;        if (config.timeoutErrorMessage) {          timeoutErrorMessage = config.timeoutErrorMessage;        }        reject(new AxiosError(          timeoutErrorMessage,          transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,          config,          req        ));        abort();      });    }        // 6)    // Send the request    if (utils.isStream(data)) {      let ended = false;      let errored = false;      data.on('end', () => {        ended = true;      });      data.once('error', err => {        errored = true;        req.destroy(err);      });      data.on('close', () => {        if (!ended && !errored) {          abort(new CanceledError('Request stream has been aborted', config, req));        }      });      data.pipe(req);    } else {      req.end(data);    }  }

是有點長,但大概瀏覽一遍就行,后面會詳細講。實現(xiàn)主要有 6 部分:86q28資訊網(wǎng)——每日最新資訊28at.com

  1. 這里的 wrapAsync 是對 return new Promise((resolve, resolve) => {}) 的包裝,暴露出 resolve、reject 供 dispatchHttpRequest 函數(shù)內(nèi)部調(diào)用使用,代表請求成功或失敗
  2. 接下里,就是根據(jù)傳入的 config 信息組裝請求參數(shù) options 了
  3. axios 會根據(jù)傳入的 url 的協(xié)議,決定是采用 http 還是 https 模塊創(chuàng)建請求
  4. 監(jiān)聽請求 req 上的異常(error)事件
  5. 跟 4) 一樣,不過監(jiān)聽的是請求 req 上的超時事件。而其他諸如取消請求、完成請求等其他兼容事件則是在 2) 創(chuàng)建請求的回調(diào)函數(shù) handleResponse(res) 中處理的
  6. 最后,調(diào)用 req.end(data) 發(fā)送請求即可。當然,這里會針對 data 是 Stream 類型的情況特別處理一下

大概介紹了之后,我們再深入每一步具體學習一下。86q28資訊網(wǎng)——每日最新資訊28at.com

包裝函數(shù) wrapAsync

首先,httpAdapter(config) 內(nèi)部的實現(xiàn)是經(jīng)過 wrapAsync 包裝函數(shù)返回的。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L122-L145const wrapAsync = (asyncExecutor) => {  return new Promise((resolve, reject) => {    let onDone;    let isDone;    const done = (value, isRejected) => {      if (isDone) return;      isDone = true;      onDone && onDone(value, isRejected);    }    const _resolve = (value) => {      done(value);      resolve(value);    };    const _reject = (reason) => {      done(reason, true);      reject(reason);    }    asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);  })};

調(diào)用 wrapAsync 函數(shù)會返回一個 Promise 對象,除了跟原生 Promise 構(gòu)造函數(shù)一樣會返回 resolve、reject 之外,還額外拓展了一個 onDone 參數(shù),確保 Promise 狀態(tài)改變后,總是會調(diào)用 onDone。86q28資訊網(wǎng)——每日最新資訊28at.com

組裝請求參數(shù)

在處理好返回值后,接下來要做的就是組裝請求參數(shù)了,請求參數(shù)最終會交由 http.request(options)[11]/https.request(options)[12] 處理,因此需要符合其類型定義。86q28資訊網(wǎng)——每日最新資訊28at.com

http 模塊的請求案例

在理解 options 參數(shù)之前,先了解一下 http 模塊的請求案例。86q28資訊網(wǎng)——每日最新資訊28at.com

const http = require('node:http');const options = {  hostname: 'www.google.com',  port: 80,  path: '/upload',  method: 'POST',  headers: {    'Content-Type': 'application/json',    'Content-Length': Buffer.byteLength(postData),  },};const req = http.request(options, (res) => {  console.log(`STATUS: ${res.statusCode}`);  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);  res.setEncoding('utf8');  res.on('data', (chunk) => {    console.log(`BODY: ${chunk}`);  });  res.on('end', () => {    console.log('No more data in response.');  });});req.on('error', (e) => {  console.error(`problem with request: ${e.message}`);});req.end(JSON.stringify({  'msg': 'Hello World!',}));

以上,我們向 http://www.google.com/upload 發(fā)起了一個 POST 請求(https 請求與此類次)。86q28資訊網(wǎng)——每日最新資訊28at.com

值得注意的是,請求參數(shù) options 中并不包含請求體數(shù)據(jù),請求體數(shù)據(jù)最終是以 req.end(data) 發(fā)動出去的,這一點跟 XMLHttpRequest 實例的做法類似。86q28資訊網(wǎng)——每日最新資訊28at.com

組裝請求參數(shù)

再來看看 axios 中關(guān)于這塊請求參數(shù)的組裝邏輯。86q28資訊網(wǎng)——每日最新資訊28at.com

首先,使用 .baseURL 和 .url 參數(shù)解析出跟 URL 相關(guān)數(shù)據(jù)。86q28資訊網(wǎng)——每日最新資訊28at.com

/v1.6.8/lib/adapters/http.js#L221// Parse urlconst fullPath = buildFullPath(config.baseURL, config.url);const parsed = new URL(fullPath, 'http://localhost');const protocol = parsed.protocol || supportedProtocols[0];

不支持的請求協(xié)議會報錯。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/platform/node/index.js#L11protocols: [ 'http', 'https', 'file', 'data' ]// /v1.6.8/lib/adapters/http.js#L44const supportedProtocols = platform.protocols.map(protocol => {  return protocol + ':';});// /v1.6.8/lib/adapters/http.js#L265-L271if (supportedProtocols.indexOf(protocol) === -1) {  return reject(new AxiosError(    'Unsupported protocol ' + protocol,    AxiosError.ERR_BAD_REQUEST,    config  ));}

錯誤 CODE 是 ERR_BAD_REQUEST,類似 4xx 錯誤。86q28資訊網(wǎng)——每日最新資訊28at.com

接下來,將 headers 參數(shù)轉(zhuǎn)成 AxiosHeaders 實例。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L273const headers = AxiosHeaders.from(config.headers).normalize();

最后,處理下請求體數(shù)據(jù) config.data。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L287-L326// support for spec compliant FormData objectsif (utils.isSpecCompliantForm(data)) {  const userBoundary = headers.getContentType(/boundary=([-_/w/d]{10,70})/i);  data = formDataToStream(data, (formHeaders) => {    headers.set(formHeaders);  }, {    tag: `axios-${VERSION}-boundary`,    boundary: userBoundary && userBoundary[1] || undefined  });  // support for https://www.npmjs.com/package/form-data api} else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {  headers.set(data.getHeaders());  if (!headers.hasContentLength()) {    try {      const knownLength = await util.promisify(data.getLength).call(data);      Number.isFinite(knownLength) && knownLength >= 0 && headers.setContentLength(knownLength);      /*eslint no-empty:0*/    } catch (e) {    }  }} else if (utils.isBlob(data)) {  data.size && headers.setContentType(data.type || 'application/octet-stream');  headers.setContentLength(data.size || 0);  data = stream.Readable.from(readBlob(data));} else if (data && !utils.isStream(data)) {  if (Buffer.isBuffer(data)) {    // Nothing to do...  } else if (utils.isArrayBuffer(data)) {    data = Buffer.from(new Uint8Array(data));  } else if (utils.isString(data)) {    data = Buffer.from(data, 'utf-8');  } else {    return reject(new AxiosError(      'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',      AxiosError.ERR_BAD_REQUEST,      config    ));  }

axios 會針對傳入的不同類型的 config.data 做統(tǒng)一處理,最終不是處理成 Stream 就是處理成 Buffer。86q28資訊網(wǎng)——每日最新資訊28at.com

不過,當傳入的 data 是對象時,在調(diào)用 httpAdapter(config) 之前,會先經(jīng)過 transformRequest() 函數(shù)處理成字符串。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/defaults/index.js#L91-L94if (isObjectPayload || hasJSONContentType ) {  headers.setContentType('application/json', false);  return stringifySafely(data);}

針對這個場景,data 會進入到下面的處理邏輯,將字符串處理成 Buffer。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L287-L326if (utils.isString(data)) {  data = Buffer.from(data, 'utf-8');}

然后,獲得請求路徑 path。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L384C4-L397C1try {  path = buildURL(    parsed.pathname + parsed.search,    config.params,    config.paramsSerializer  ).replace(/^/?/, '');} catch (err) {   // ...}

最后,組裝 options 參數(shù)。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L403C1-L413C7const options = {  path,  method: method,  headers: headers.toJSON(),  agents: { http: config.httpAgent, https: config.httpsAgent },  auth,  protocol,  family,  beforeRedirect: dispatchBeforeRedirect,  beforeRedirects: {}};

創(chuàng)建請求

再看創(chuàng)建請求環(huán)節(jié)。86q28資訊網(wǎng)——每日最新資訊28at.com

獲得請求實例

首先,是獲得請求實例。86q28資訊網(wǎng)——每日最新資訊28at.com

import followRedirects from 'follow-redirects';const {http: httpFollow, https: httpsFollow} = followRedirects;// /v1.6.8/lib/adapters/http.js#L426-L441let transport;const isHttpsRequest = isHttps.test(options.protocol);options.agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;if (config.transport) {  transport = config.transport;} else if (config.maxRedirects === 0) {  transport = isHttpsRequest ? https : http;} else {  if (config.maxRedirects) {    options.maxRedirects = config.maxRedirects;  }  if (config.beforeRedirect) {    options.beforeRedirects.config = config.beforeRedirect;  }  transport = isHttpsRequest ? httpsFollow : httpFollow;}

如上所示,你可以通過 config.transport 傳入,但通常不會這么做。否則,axios 內(nèi)部會根據(jù)你是否傳入 config.maxRedirects(默認 undefined) 決定使用原生 http/https 模塊還是 follow-redirects 包里提供的 http/https 方法。86q28資訊網(wǎng)——每日最新資訊28at.com

如果沒有傳入 config.maxRedirects,axios 默認會使用 follow-redirects 包里提供的 http/https 方法發(fā)起請求,它的用法跟原生 http/https 模塊一樣,這里甚至可以只使用 follow-redirects 就夠了。86q28資訊網(wǎng)——每日最新資訊28at.com

創(chuàng)建請求

下面就是創(chuàng)建請求了。86q28資訊網(wǎng)——每日最新資訊28at.com

// Create the requestreq = transport.request(options, function handleResponse(res) {}

我們在 handleResponse 回調(diào)函數(shù)里處理返回數(shù)據(jù) res。86q28資訊網(wǎng)——每日最新資訊28at.com

function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;function request(    url: string | URL,    options: RequestOptions,    callback?: (res: IncomingMessage) => void,): ClientRequest;

根據(jù)定義,我們知道 res 是 IncomingMessage 類型,繼承自 stream.Readable[13],是一種可讀的 Stream。86q28資訊網(wǎng)——每日最新資訊28at.com

const readable = getReadableStreamSomehow();readable.on('data', (chunk) => {  console.log(`Received ${chunk.length} bytes of data.`);});

res 的處理我們會放到處理請求一節(jié)講述,下面就是發(fā)出請求了。86q28資訊網(wǎng)——每日最新資訊28at.com

發(fā)出請求

這部分代碼比較簡單,而數(shù)據(jù)體也是在這里傳入的。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L658C5-L681C6// Send the requestif (utils.isStream(data)) {  let ended = false;  let errored = false;  data.on('end', () => {    ended = true;  });  data.once('error', err => {    errored = true;    req.destroy(err);  });  data.on('close', () => {    if (!ended && !errored) {      abort(new CanceledError('Request stream has been aborted', config, req));    }  });  data.pipe(req);} else {  req.end(data);}

如果你的請求體是 Buffer 類型的,那么直接傳入 req.end(data) 即可,否則(Stream 類型)則需要以管道形式傳遞給 req。86q28資訊網(wǎng)——每日最新資訊28at.com

處理請求

接著創(chuàng)建請求一節(jié),下面開始分析請求的處理。86q28資訊網(wǎng)——每日最新資訊28at.com

Node.js 部分的請求處理,比處理 XMLHttpRequest 稍微復雜一些。你要在 2 個地方做監(jiān)聽處理。86q28資訊網(wǎng)——每日最新資訊28at.com

  1. transport.request 返回的 req 實例
  2. 另一個,則是 transport.request 回調(diào)函數(shù) handleResponse 返回的 res(也就是 responseStream)

監(jiān)聽 responseStream

首先,用 res/responseStream 上已有的信息組裝響應(yīng)數(shù)據(jù) response。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L478// decompress the response body transparently if requiredlet responseStream = res;// return the last request in case of redirectsconst lastRequest = res.req || req;const response = {  status: res.statusCode,  statusText: res.statusMessage,  headers: new AxiosHeaders(res.headers),  config,  request: lastRequest};

這是不完整的,因為我們還沒有設(shè)置 response.data。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L535C7-L538C15if (responseType === 'stream') {  response.data = responseStream;  settle(resolve, reject, response);} else {  // ...}

如果用戶需要的是響應(yīng)類型是 stream,那么一切就變得簡單了,直接將數(shù)據(jù)都給 settle 函數(shù)即可。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/core/settle.jsexport default function settle(resolve, reject, response) {  const validateStatus = response.config.validateStatus;  if (!response.status || !validateStatus || validateStatus(response.status)) {    resolve(response);  } else {    reject(new AxiosError(      'Request failed with status code ' + response.status,      [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],      response.config,      response.request,      response    ));  }}

settle 函數(shù)會根據(jù)傳入的 response.status 和 config.validateStatus() 決定請求是成功(resolve)還是失?。╮eject)。86q28資訊網(wǎng)——每日最新資訊28at.com

當然,如果需要的響應(yīng)類型不是 stream,就監(jiān)聽 responseStream 對象上的事件,處理請求結(jié)果。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L538C1-L591C8} else {  const responseBuffer = [];  let totalResponseBytes = 0;  // 1)  responseStream.on('data', function handleStreamData(chunk) {    responseBuffer.push(chunk);    totalResponseBytes += chunk.length;    // make sure the content length is not over the maxContentLength if specified    if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {      // stream.destroy() emit aborted event before calling reject() on Node.js v16      rejected = true;      responseStream.destroy();      reject(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',        AxiosError.ERR_BAD_RESPONSE, config, lastRequest));    }  });    // 2)  responseStream.on('aborted', function handlerStreamAborted() {    if (rejected) {      return;    }    const err = new AxiosError(      'maxContentLength size of ' + config.maxContentLength + ' exceeded',      AxiosError.ERR_BAD_RESPONSE,      config,      lastRequest    );    responseStream.destroy(err);    reject(err);  });  // 3)  responseStream.on('error', function handleStreamError(err) {    if (req.destroyed) return;    reject(AxiosError.from(err, null, config, lastRequest));  });    // 4)  responseStream.on('end', function handleStreamEnd() {    try {      let responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);      if (responseType !== 'arraybuffer') {        responseData = responseData.toString(responseEncoding);        if (!responseEncoding || responseEncoding === 'utf8') {          responseData = utils.stripBOM(responseData);        }      }      response.data = responseData;    } catch (err) {      return reject(AxiosError.from(err, null, config, response.request, response));    }    settle(resolve, reject, response);  });}

responseStream 上會監(jiān)聽 4 個事件。86q28資訊網(wǎng)——每日最新資訊28at.com

  1. data:Node 請求的響應(yīng)默認都是以流數(shù)據(jù)形式接收的,而 data 就是在接收過程中會不斷觸發(fā)的事件。我們在這里將接收到的數(shù)據(jù)存儲在 responseBuffer 中,以便后續(xù)使用
  2. aborted:會在接收響應(yīng)數(shù)據(jù)超過時,或是調(diào)用 .destory() 時觸發(fā)
  3. err:在流數(shù)據(jù)接收錯誤時調(diào)用
  4. end:數(shù)據(jù)結(jié)束接收,將收集到的 responseBuffer 先轉(zhuǎn)換成 Buffer 類型,再轉(zhuǎn)換成字符串,最終賦值給 response.data

監(jiān)聽 req

以上,我們完成了對響應(yīng)數(shù)據(jù)的監(jiān)聽。我們再來看看,對請求實例 req 的監(jiān)聽。86q28資訊網(wǎng)——每日最新資訊28at.com

// /v1.6.8/lib/adapters/http.js#L606// Handle errorsreq.on('error', function handleRequestError(err) {  // @todo remove  // if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;  reject(AxiosError.from(err, null, config, req));});// /v1.6.8/lib/adapters/http.js#L619// Handle request timeoutif (config.timeout) {  req.setTimeout(timeout, function handleRequestTimeout() {    if (isDone) return;    let timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';    const transitional = config.transitional || transitionalDefaults;    if (config.timeoutErrorMessage) {      timeoutErrorMessage = config.timeoutErrorMessage;    }    reject(new AxiosError(      timeoutErrorMessage,      transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,      config,      req    ));    abort();  });}

一共監(jiān)聽了 2 個事件:86q28資訊網(wǎng)——每日最新資訊28at.com

  1. error:請求出錯
  2. req.setTimeout():請求超時

以上,我們就完成了請求處理的所有內(nèi)容。可以發(fā)現(xiàn),Node 端處理請求的邏輯會比瀏覽器端稍微復雜一些:你需要同時監(jiān)聽請求實例以及響應(yīng)流數(shù)據(jù)上的事件,確保整個請求過程被完整監(jiān)聽。86q28資訊網(wǎng)——每日最新資訊28at.com

總結(jié)

本文主要帶大家學習了 axios 的 Node 端實現(xiàn)。86q28資訊網(wǎng)——每日最新資訊28at.com

相比較于瀏覽器端要稍微復雜一些,不僅是因為我們要考慮請求可能的最大跳轉(zhuǎn)(maxRedirects),還要同時監(jiān)聽請求實例以及響應(yīng)流數(shù)據(jù)上的事件,確保整個請求過程被完整監(jiān)聽。86q28資訊網(wǎng)——每日最新資訊28at.com

參考資料

[1]axios 是如何實現(xiàn)取消請求的?: https://juejin.cn/post/735944401389481168986q28資訊網(wǎng)——每日最新資訊28at.com

[2]你知道嗎?axios 請求是 JSON 響應(yīng)優(yōu)先的: https://juejin.cn/post/735958060532003641586q28資訊網(wǎng)——每日最新資訊28at.com

[3]axios 跨端架構(gòu)是如何實現(xiàn)的?: https://juejin.cn/post/736211984866045139186q28資訊網(wǎng)——每日最新資訊28at.com

[4]axios 攔截器機制是如何實現(xiàn)的?: https://juejin.cn/post/736354573787416170386q28資訊網(wǎng)——每日最新資訊28at.com

[5]axios 瀏覽器端請求是如何實現(xiàn)的?: https://juejin.cn/post/736392856902882102986q28資訊網(wǎng)——每日最新資訊28at.com

[6]axios 對外出口API是如何設(shè)計的?: https://juejin.cn/post/736461433737130807186q28資訊網(wǎng)——每日最新資訊28at.com

[7]axios 中是如何處理異常的?: https://juejin.cn/post/736995108519473977586q28資訊網(wǎng)——每日最新資訊28at.com

[8]axios 內(nèi)置了 2 個適配器(截止到 v1.6.8 版本): https://github.com/axios/axios/tree/v1.6.8/lib/adapters86q28資訊網(wǎng)——每日最新資訊28at.com

[9]--watch: https://nodejs.org/api/cli.html#--watch86q28資訊網(wǎng)——每日最新資訊28at.com

[10]lib/adapters/http.js: https://github.com/axios/axios/blob/v1.6.8/lib/adapters/http.js86q28資訊網(wǎng)——每日最新資訊28at.com

[11]http.request(options): https://nodejs.org/docs/latest/api/http.html#httprequestoptions-callback86q28資訊網(wǎng)——每日最新資訊28at.com

[12]https.request(options): https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback86q28資訊網(wǎng)——每日最新資訊28at.com

[13]stream.Readable: https://nodejs.org/docs/latest/api/stream.html#class-streamreadable86q28資訊網(wǎng)——每日最新資訊28at.com

86q28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-92115-0.htmlAxios Node 端請求是如何實現(xiàn)的?

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Go 新提案:用 #err 標識符去做錯誤處理!

下一篇: 被嚴重低估!React 19 又是一次開發(fā)方式的變革,useEffect 將會逐漸退出歷史舞臺

標簽:
  • 熱門焦點
Top