cross-fetch vs isomorphic-fetch vs axios vs node-fetch
HTTP请求库
cross-fetchisomorphic-fetchaxiosnode-fetch类似的npm包:

HTTP请求库

HTTP请求库是用于在JavaScript应用程序中发送HTTP请求的工具,它们简化了与服务器的通信过程。通过这些库,开发者可以方便地进行数据获取、提交表单、处理响应等操作。选择合适的HTTP请求库可以提高开发效率和代码的可维护性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
cross-fetch30,537,2111,69893.3 kB271 年前MIT
isomorphic-fetch8,078,0336,925-576 年前MIT
axios0108,6582.41 MB3551 天前MIT
node-fetch08,861107 kB2403 年前MIT

功能对比: cross-fetch vs isomorphic-fetch vs axios vs node-fetch

功能丰富性

  • cross-fetch:

    Cross-fetch是一个轻量级的库,主要提供Fetch API的polyfill,功能相对简单,适合需要基本HTTP请求功能的项目。

  • isomorphic-fetch:

    Isomorphic-fetch提供了一个统一的Fetch API接口,适合需要在服务端和客户端共享代码的同构应用。它的功能与Fetch API相似,但并不提供额外的功能。

  • axios:

    Axios提供了丰富的功能,包括请求和响应拦截、请求取消、自动转换JSON数据、以及支持Promise API。它还允许你设置全局配置,方便管理请求的默认行为。

  • node-fetch:

    Node-fetch是一个轻量级的Fetch API实现,专注于Node.js环境,功能简单,适合只需基本HTTP请求的场景。

兼容性

  • cross-fetch:

    Cross-fetch是一个polyfill,旨在提供Fetch API的兼容性,能够在不支持Fetch的环境中使用,适合需要兼容性解决方案的项目。

  • isomorphic-fetch:

    Isomorphic-fetch的设计目标是提供一个在浏览器和Node.js中都能使用的Fetch API,确保同构应用的代码一致性。

  • axios:

    Axios在浏览器和Node.js环境中都能良好工作,支持Promise和async/await语法,兼容性强。

  • node-fetch:

    Node-fetch专为Node.js环境设计,提供了与浏览器Fetch API相似的接口,适合Node.js开发者使用。

学习曲线

  • cross-fetch:

    Cross-fetch的使用方式与Fetch API相似,学习曲线较低,适合已经熟悉Fetch的开发者。

  • isomorphic-fetch:

    Isomorphic-fetch的学习曲线与Fetch API相似,适合需要在同构应用中使用Fetch的开发者。

  • axios:

    Axios的API设计直观易懂,学习曲线相对平缓,适合新手和经验丰富的开发者。

  • node-fetch:

    Node-fetch的API与Fetch API相似,学习曲线较低,适合熟悉Fetch的Node.js开发者。

社区支持

  • cross-fetch:

    Cross-fetch的社区相对较小,但由于其简单性,文档清晰,易于上手。

  • isomorphic-fetch:

    Isomorphic-fetch的社区支持有限,但由于其与Fetch API的相似性,开发者可以容易找到相关资源。

  • axios:

    Axios拥有一个活跃的社区,提供了丰富的文档和示例,易于获取支持和资源。

  • node-fetch:

    Node-fetch在Node.js社区中受到广泛使用,拥有良好的文档和支持,适合Node.js开发者。

性能

  • cross-fetch:

    Cross-fetch的性能与原生Fetch API相似,适合需要基本性能的项目。

  • isomorphic-fetch:

    Isomorphic-fetch的性能与Fetch API相似,适合需要在服务端和客户端共享代码的应用。

  • axios:

    Axios在性能上表现良好,支持请求和响应拦截,能够优化网络请求的效率。

  • node-fetch:

    Node-fetch的性能优越,专为Node.js环境优化,适合高性能的HTTP请求场景。

如何选择: cross-fetch vs isomorphic-fetch vs axios vs node-fetch

  • cross-fetch:

    选择Cross-fetch如果你需要一个轻量级的库,能够在浏览器和Node.js环境中无缝工作。它是Fetch API的一个polyfill,提供了一个统一的接口,适合需要兼容性和简洁性的项目。

  • isomorphic-fetch:

    选择Isomorphic-fetch如果你需要一个可以在浏览器和Node.js中都使用的Fetch API实现。它允许你在同一代码库中使用相同的API进行服务端和客户端请求,适合同构应用。

  • axios:

    选择Axios如果你需要一个功能丰富的库,支持请求和响应拦截、请求取消、自动转换JSON数据等功能。Axios在浏览器和Node.js环境中都能良好工作,并且具有良好的社区支持。

  • node-fetch:

    选择Node-fetch如果你只在Node.js环境中工作,并且需要一个轻量级的Fetch API实现。它专注于Node.js的使用,提供了与浏览器Fetch API相似的接口。

cross-fetch的README

cross-fetch
NPM Version Downloads Per Week License: MIT CI codecov

Universal WHATWG Fetch API for Node, Browsers, Workers and React Native. The scenario that cross-fetch really shines is when the same JavaScript codebase needs to run on different platforms.

  • Platform agnostic: browsers, Node or React Native
  • Optional polyfill: it's up to you if something is going to be added to the global object or not
  • Simple interface: no instantiation, no configuration and no extra dependency
  • WHATWG compliant: it works the same way wherever your code runs
  • TypeScript support: better development experience with types.
  • Worker support: works on different types of workers such as Service Workers and CloudFlare Workers

Table of Contents


Install

npm install --save cross-fetch

As a ponyfill (imports locally):

// Using ES6 modules with Babel or TypeScript
import fetch from 'cross-fetch';

// Using CommonJS modules
const fetch = require('cross-fetch');

As a polyfill (installs globally):

// Using ES6 modules
import 'cross-fetch/polyfill';

// Using CommonJS modules
require('cross-fetch/polyfill');

The CDN build is also available on unpkg:

<script src="//unpkg.com/cross-fetch/dist/cross-fetch.js"></script>

This adds the fetch function to the window object. Note that this is not UMD compatible.


Usage

With promises:

import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';

fetch('//api.github.com/users/lquixada')
  .then(res => {
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    return res.json();
  })
  .then(user => {
    console.log(user);
  })
  .catch(err => {
    console.error(err);
  });

With async/await:

import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';

(async () => {
  try {
    const res = await fetch('//api.github.com/users/lquixada');
    
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    
    const user = await res.json();
  
    console.log(user);
  } catch (err) {
    console.error(err);
  }
})();

Demo & API

You can find a comprehensive doc at Github's fetch page. If you want to play with cross-fetch, check our JSFiddle playground.

Tip: Run the fiddle on various browsers and with different settings (for instance: cross-domain requests, wrong urls or text requests). Don't forget to open the console in the test suite page and play around.

FAQ

Yet another fetch library?

I did a lot of research in order to find a fetch library that could be simple, cross-platform and provide polyfill as an option. There's a plethora of libs out there but none could match those requirements.

Why polyfill might not be a good idea?

In a word? Risk. If the spec changes in the future, it might be problematic to debug. Read more about it on sindresorhus's ponyfill page. It's up to you if you're fine with it or not.

How does cross-fetch work?

Just like isomorphic-fetch, it is just a proxy. If you're in node, it delivers you the node-fetch library, if you're in a browser or React Native, it delivers you the github's whatwg-fetch. The same strategy applies whether you're using polyfill or ponyfill.

Who's Using It?

The New York TimesApollo GraphQLFacebookSwaggerVulcanJSgraphql-request
The New York TimesApollo GraphQLFacebookSwaggerVulcanJSgraphql-request

Thanks

Heavily inspired by the works of matthew-andrews. Kudos to him!

License

cross-fetch is licensed under the MIT license © Leonardo Quixadá

Author

@lquixada
@lquixada