@derhuerst/http-basic

中文文档
8.2.4发布于 2022/07/04MITTypeScriptCommonJS近 30 天下载 1285万

Very low level wrapper arround http.request/https.request

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Implementing a Cache
  4. License

http-basic

This is a temporary fork of ForbesLindesay/http-basic.


Simple wrapper arround http.request/https.request

Build Status Dependency Status NPM version

Installation

npm install http-basic

Usage

var request = require('http-basic');

var options = {followRedirects: true, gzip: true, cache: 'memory'};

var req = request('GET', 'http://example.com', options, function (err, res) {
  if (err) throw err;
  console.dir(res.statusCode);
  res.body.resume();
});
req.end();

method:

The http method (e.g. GET, POST, PUT, DELETE etc.)

url:

The url as a string (e.g. http://example.com). It must be fully qualified and either http or https.

options:

  • headers - (default {}) http headers
  • agent - (default: false) controlls keep-alive (see http://nodejs.org/api/http.html#http_http_request_options_callback)
  • duplex - (default: true except for GET, OPTIONS and HEAD requests) allows you to explicitly set a body on a request that uses a method that normally would not have a body
  • followRedirects - (default: false) - if true, redirects are followed (note that this only affects the result in the callback)
  • maxRedirects - (default: Infinity) - limit the number of redirects allowed.
  • allowRedirectHeaders (default: null) - an array of headers allowed for redirects (none if null).
  • gzip (default: false) - automatically accept gzip and deflate encodings. This is kept completely transparent to the user.
  • cache - (default: null) - 'memory' or 'file' to use the default built in caches or you can pass your own cache implementation.
  • timeout (default: false) - times out if no response is returned within the given number of milliseconds.
  • socketTimeout (default: false) - calls req.setTimeout internally which causes the request to timeout if no new data is seen for the given number of milliseconds.
  • retry (default: false) - retry GET requests. Set this to true to retry when the request errors or returns a status code greater than or equal to 400 (can also be a function that takes (err, req, attemptNo) => shouldRetry)
  • retryDelay (default: 200) - the delay between retries (can also be set to a function that takes (err, res, attemptNo) => delay)
  • maxRetries (default: 5) - the number of times to retry before giving up.
  • ignoreFailedInvalidation (default: false) - whether the cache should swallow errors if there is a problem removing a cached response. Note that enabling this setting may result in incorrect, cached data being returned to the user.
  • isMatch - (requestHeaders: Headers, cachedResponse: CachedResponse, defaultValue: boolean) => boolean - override the default behaviour for testing whether a cached response matches a request.
  • isExpired - (cachedResponse: CachedResponse, defaultValue: boolean) => boolean - override the default behaviour for testing whether a cached response has expired
  • canCache - (res: Response<NodeJS.ReadableStream>, defaultValue: boolean) => boolean - override the default behaviour for testing whether a response can be cached

callback:

The callback is called with err as the first argument and res as the second argument. res is an http-response-object. It has the following properties:

  • statusCode - a number representing the HTTP Status Code
  • headers - an object representing the HTTP headers
  • body - a readable stream respresenting the request body.
  • url - the URL that was requested (in the case of redirects, this is the final url that was requested)

returns:

If the method is GET, DELETE or HEAD, it returns undefined.

Otherwise, it returns a writable stream for the body of the request.

Implementing a Cache

A Cache is an object with three methods:

  • getResponse(url, callback) - retrieve a cached response object
  • setResponse(url, response) - cache a response object
  • invalidateResponse(url, callback) - remove a response which is no longer valid

A cached response object is an object with the following properties:

  • statusCode - Number
  • headers - Object (key value pairs of strings)
  • body - Stream (a stream of binary data)
  • requestHeaders - Object (key value pairs of strings)
  • requestTimestamp - Number

getResponse should call the callback with an optional error and either null or a cached response object, depending on whether the url can be found in the cache. Only GETs are cached.

setResponse should just swallow any errors it has (or resport them using console.warn).

invalidateResponse should call the callback with an optional error if it is unable to invalidate a response.

A cache may also define any of the methods from lib/cache-utils.js to override behaviour for what gets cached. It is currently still only possible to cache "get" requests, although this could be changed.

License

MIT

版本与发布记录

@derhuerst/http-basic 共发布 5 个版本,首次发布于 2020/04/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
8.2.4latest2022/07/044 年前
8.2.32022/06/274 年前
8.2.22022/01/034 年前
8.2.12020/09/166 年前
8.2.02020/04/066 年前

关于 @derhuerst/http-basic 的常见问题

@derhuerst/http-basic 是什么?

Very low level wrapper arround http.request/https.request

如何安装 @derhuerst/http-basic?

在项目目录运行 npm install @derhuerst/http-basic,或使用 pnpm add / yarn add / bun add。

@derhuerst/http-basic 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@derhuerst/http-basic 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@derhuerst/http-basic 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@derhuerst/http-basic 可以商用吗?

@derhuerst/http-basic 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@derhuerst/http-basic 的下载量有多少?

根据 npm 官方下载统计,@derhuerst/http-basic 在 2026/10/12 – 2026/11/10 这 30 天内被下载 12,849,908 次,日均约 428,330 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@derhuerst/http-basic 还在维护吗?

最新版本 8.2.4 发布于 2022/07/04(4 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/04/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。