@acheetahk/request 中文文档教程
#
@acheetahk/request
@acheetahk/request ⛵ . supports all node.js request methods for HTTP/HTTPS and includes the best proxy and file download modules.
请使用2.9.0以上的版本
Methods Nav
request
关于
proxy
关于
文件
Installation
npm install @acheetahk/request
cnpm install @acheetahk/request
yarn add @acheetahk/request
Dependencies
{
"@types/express": "^4.17.8",
"@types/form-data": "^2.5.0",
"axios": "^0.21.0",
"form-data": "^3.0.0"
}
Usage
fastRequest
fastRequest - get
import { fastRequest } from '@acheetahk/request';
const response = await fastRequest({ url: <YOUR URL>, method: 'GET' });
fastRequest - post
import { fastRequest } from '@acheetahk/request';
const response = await fastRequest({ url: <YOUR URL>, method: 'POST', data: { value: 'test post' } });
fastRequest - options
|参数|类型|解释|要求| |:-:|:-:|:-:|:-:| |url
|string|url|true| |方法
|GET
DELETE
HEAD
OPTIONS
POST
PUT
PATCH
PURGE
LINK
UNLINK
|method|true| |headers
|any|header|false| |query
|any|连接 URL 中的参数|false| |data
|any|data
仅适用于这些请求方法 'PUT'、'POST' 和 'PATCH'|false| |timeout
|number|timeout 单位是秒|false| |withCredentials
|boolean|允许cookie信息跨域携带|false| |responseType
|arraybuffer
blob
文档
json
文本
流
|响应类型|false| |onUploadProgress
|(progressEvent: any) => void|配置上传进度|false| |onDownloadProgress
|(progressEvent: any) => void|配置下载进度|false| |代理
|{ host
: string, port
: number, auth
: { username
: 字符串, 密码
: 字符串 } }| 配置代理 |false| |解压
|boolean| 指示是否应解压缩响应主体 |false|
fastFormData
fastFormData - example
import * as fs from 'fs';
import { fastFormData } from '@acheetahk/request';
const response = await fastFormData(
{
url,
data: {
name: 'test formData',
file: fs.createReadStream(resolve(__dirname, './xx.xx')),
},
},
);
fastFormData - options
|参数|类型|解释|要求| |:-:|:-:|:-:|:-:| |url
|string|要寻址的资源|true| |data
|any|form-data body
|true| |配置
|RequestOptions
| ⬇️ |真|
fastFormData - options - configs
|参数|类型|解释|要求| |:-:|:-:|:-:|:-:| |headers
|any|header|false| |timeout
|number|timeout 单位是秒|false| |withCredentials
|boolean|允许cookie信息跨域携带|false| |responseType
|arraybuffer
blob
文档
json
文本
流
|响应类型|false| |onUploadProgress
|(progressEvent: any) => void|配置上传进度|false| |onDownloadProgress
|(progressEvent: any) => void|配置下载进度|false| |解压
|boolean| 指示是否应解压缩响应主体 |false|
fastProxy
委托给指定的 express.js / nest.js 服务
fastProxy - example for nest.js
import { NestFactory } from '@nestjs/core';
import { Module, All, Controller, HttpCode, HttpStatus, InternalServerErrorException, Req } from '@nestjs/common';
@Controller('demo')
class DemoController {
@All()
@HttpCode(HttpStatus.OK)
async demo(@Req() requset: Request) {
try {
await fastProxy(
requset,
requset.res,
{
host: '<The server host that requires a proxy>',
port: '<The server port that requires a proxy>',
isRewrite: true,
rewrite: '/demo',
body: requset.body,
path: requset.originalUrl,
headers: requset.headers,
},
);
} catch (error) {
throw new InternalServerErrorException('Error');
}
}
}
const app = await NestFactory.create(CoreModule, { cors: true });
await app.listen(8000);
fastProxy - example of express.js
import * as express from 'express';
import { fastProxy } from '@acheetahk/request';
const app = express();
const router = express.Router();
app.use('/express', router);
router.all('*', async (requset: Request, response: Response) => {
await fastProxy(
requset,
response,
{
host: '<The server host that requires a proxy>',
port: '<The server port that requires a proxy>',
isRewrite: true,
rewrite: '/express',
body: requset.body,
path: requset.originalUrl,
headers: requset.headers,
},
);
});
const server = app.listen(8000);
fastProxy - options
|param|type|explain|require| |:-:|:-:|:-:|:-:| |req
|express.request
|底层是HTTP.IncomingMessage|true| |res
|express.response
|底层是HTTP.ServerResponse|true| |选项
|ProxyOptions
| ⬇️ |真|
fastProxy - options - options
|参数|类型|解释|要求| |:-:|:-:|:-:|:-:| |<代码>主机| string |需要代理的服务器主机|true| |<代码>端口| number |需要代理的服务器端口|true| |path
|string|路由地址|true| |headers
|any|请求标头|true| |<代码>正文| any |请求体|false| |timeout
|number|timeout 单位是秒|false| |isRewrite
|boolean|是否覆盖路由地址|false| |rewrite
|string|重写规则示例:'/express' ==> '/'
或 '/express/test' ==> '/测试'
|false|
fileDownload
fileDownload - example
下载文件返回本地文件路径
import { resolve } from 'path';
const path = resolve(__dirname, './fileDownload');
const url = <YOUR FILE URL>;
// result -> The full path of the downloaded file
const result = await fileDownload(url, path);
fileDownload - args
|param|type|require| |:-:|:-:|:-:| |网址|字符串|真| |路径|字符串|真|
fileToStr
fileToStr - example
将网络文件转换成指定编码的字符串
const url = <YOUR FILE URL>;
const result = await fileToStr(url);
fileToStr - args
|param|type|explain|require| |:-:|:-:|:-:|:-:| |网址|字符串|网址|真| |type|string|编码类型,默认为base64
|true|
fileToBuffer
fileToBuffer - example
将网页文件转换为缓冲区
const url = <YOUR FILE URL>;
const result = await fileToBuffer(url, path);
fileToBuffer - args
|param|type|require| |:-:|:-:|:-:| |网址|字符串|真|
fileToDuplexStream
fileToDuplexStream - example
将网页文件转换为双工流
const url = <YOUR FILE URL>;
const result = await fileToDuplexStream(url, path);
fileToDuplexStream - args
|param|type|require| |:-:|:-:|:-:| |网址|字符串|真|
bufferToStream
bufferToStream - example
const result = await bufferToStream(buffer);
bufferToStream - args
|参数|类型|要求| |:-:|:-:|:-:| |缓冲区|缓冲区|真|
#
@acheetahk/request
@acheetahk/request ⛵ . supports all node.js request methods for HTTP/HTTPS and includes the best proxy and file download modules.
Please use version greater than 2.9.0
Methods Nav
about
request
about
proxy
about
file
Installation
npm install @acheetahk/request
cnpm install @acheetahk/request
yarn add @acheetahk/request
Dependencies
{
"@types/express": "^4.17.8",
"@types/form-data": "^2.5.0",
"axios": "^0.21.0",
"form-data": "^3.0.0"
}
Usage
fastRequest
fastRequest - get
import { fastRequest } from '@acheetahk/request';
const response = await fastRequest({ url: <YOUR URL>, method: 'GET' });
fastRequest - post
import { fastRequest } from '@acheetahk/request';
const response = await fastRequest({ url: <YOUR URL>, method: 'POST', data: { value: 'test post' } });
fastRequest - options
|param|type|explain|require| |:-:|:-:|:-:|:-:| |url
|string|url|true| |method
|GET
DELETE
HEAD
OPTIONS
POST
PUT
PATCH
PURGE
LINK
UNLINK
|method|true| |headers
|any|header|false| |query
|any|Concatenate parameters in the URL|false| |data
|any|data
Apply only to these request methods 'PUT', 'POST', and 'PATCH'|false| |timeout
|number|timeout Units are seconds|false| |withCredentials
|boolean|allows cookie information to be carried across domains|false| |responseType
|arraybuffer
blob
document
json
text
stream
|response-type|false| |onUploadProgress
|(progressEvent: any) => void|Configure the progress on upload|false| |onDownloadProgress
|(progressEvent: any) => void|Configure the progress on download|false| |proxy
|{ host
: string, port
: number, auth
: { username
: string, password
: string } }| Configure proxy |false| |decompress
|boolean| indicates whether or not the response body should be decompressed |false|
fastFormData
fastFormData - example
import * as fs from 'fs';
import { fastFormData } from '@acheetahk/request';
const response = await fastFormData(
{
url,
data: {
name: 'test formData',
file: fs.createReadStream(resolve(__dirname, './xx.xx')),
},
},
);
fastFormData - options
|param|type|explain|require| |:-:|:-:|:-:|:-:| |url
|string|Resources to address|true| |data
|any|form-data body
|true| |configs
|RequestOptions
| ⬇️ ???? |true|
fastFormData - options - configs
|param|type|explain|require| |:-:|:-:|:-:|:-:| |headers
|any|header|false| |timeout
|number|timeout Units are seconds|false| |withCredentials
|boolean|allows cookie information to be carried across domains|false| |responseType
|arraybuffer
blob
document
json
text
stream
|response-type|false| |onUploadProgress
|(progressEvent: any) => void|Configure the progress on upload|false| |onDownloadProgress
|(progressEvent: any) => void|Configure the progress on download|false| |decompress
|boolean| indicates whether or not the response body should be decompressed |false|
fastProxy
Delegate to the specified express.js / nest.js service
fastProxy - example for nest.js
import { NestFactory } from '@nestjs/core';
import { Module, All, Controller, HttpCode, HttpStatus, InternalServerErrorException, Req } from '@nestjs/common';
@Controller('demo')
class DemoController {
@All()
@HttpCode(HttpStatus.OK)
async demo(@Req() requset: Request) {
try {
await fastProxy(
requset,
requset.res,
{
host: '<The server host that requires a proxy>',
port: '<The server port that requires a proxy>',
isRewrite: true,
rewrite: '/demo',
body: requset.body,
path: requset.originalUrl,
headers: requset.headers,
},
);
} catch (error) {
throw new InternalServerErrorException('Error');
}
}
}
const app = await NestFactory.create(CoreModule, { cors: true });
await app.listen(8000);
fastProxy - example of express.js
import * as express from 'express';
import { fastProxy } from '@acheetahk/request';
const app = express();
const router = express.Router();
app.use('/express', router);
router.all('*', async (requset: Request, response: Response) => {
await fastProxy(
requset,
response,
{
host: '<The server host that requires a proxy>',
port: '<The server port that requires a proxy>',
isRewrite: true,
rewrite: '/express',
body: requset.body,
path: requset.originalUrl,
headers: requset.headers,
},
);
});
const server = app.listen(8000);
fastProxy - options
|param|type|explain|require| |:-:|:-:|:-:|:-:| |req
|express.request
|The bottom layer is HTTP.IncomingMessage|true| |res
|express.response
|The bottom layer is HTTP.ServerResponse|true| |options
|ProxyOptions
| ⬇️ ???? |true|
fastProxy - options - options
|param|type|explain|require| |:-:|:-:|:-:|:-:| |host
| string |The server host that requires a proxy|true| |port
| number |The server port that requires a proxy|true| |path
|string|The routing address|true| |headers
|any|The request header|true| |body
| any |The request body|false| |timeout
|number|timeout Units are seconds|false| |isRewrite
|boolean|Whether to override the routing address|false| |rewrite
|string|example of rewrite rule: '/express' ==> '/'
or '/express/test' ==> '/test'
|false|
fileDownload
fileDownload - example
Download the file and return to the local file path
import { resolve } from 'path';
const path = resolve(__dirname, './fileDownload');
const url = <YOUR FILE URL>;
// result -> The full path of the downloaded file
const result = await fileDownload(url, path);
fileDownload - args
|param|type|require| |:-:|:-:|:-:| |url|string|true| |path|string|true|
fileToStr
fileToStr - example
Convert the network file into a string of the specified encoding
const url = <YOUR FILE URL>;
const result = await fileToStr(url);
fileToStr - args
|param|type|explain|require| |:-:|:-:|:-:|:-:| |url|string|url|true| |type|string|Encoding type, the default is base64
|true|
fileToBuffer
fileToBuffer - example
Convert web files to Buffer
const url = <YOUR FILE URL>;
const result = await fileToBuffer(url, path);
fileToBuffer - args
|param|type|require| |:-:|:-:|:-:| |url|string|true|
fileToDuplexStream
fileToDuplexStream - example
Convert web files to Duplex Stream
const url = <YOUR FILE URL>;
const result = await fileToDuplexStream(url, path);
fileToDuplexStream - args
|param|type|require| |:-:|:-:|:-:| |url|string|true|
bufferToStream
bufferToStream - example
const result = await bufferToStream(buffer);
bufferToStream - args
|param|type|require| |:-:|:-:|:-:| |buffer|Buffer|true|