npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

hongfangze-http-request

v1.0.1

Published

comm.http-request

Readme

HTTP请求帮助类

介绍

发送HTTP请求、代理API接口等

  1. 一些URL相关的函数

    import {receiverRequestBody,receiverRequestFormData,getUrlParams} from "hongfangze-http-request";
    
    /**
     * 解析HTTP请求数据包
     * @param {IncomingMessage} req express/nest.js等框架或Node.js原生的HTTP请求对象
     * @return {*}  {Promise<string | Record<string, any>>}
     */
    export declare const receiverRequestBody: (req: IncomingMessage) => Promise<string | Record<string, any>>;
    
    /**
     * 获取HTTP请求FormData数据
     * @param {IncomingMessage} req express/nest.js等框架或Node.js原生的HTTP请求对象
     * @return {Promise<{ form: any, tmpFiles: string[] }>} form信息及form中的文件临时存放地址,使用完后请自行删除
     */
    export declare const receiverRequestFormData: (req: IncomingMessage) => Promise<{
        form: any;
        tmpFiles: string[];
    }>;
    
    /**
     * 获取HTTP请求地址的参数
     * @param {string} url 地址
     * @param {string} [name] 参数名
     * @return {*}  {(string | Record<string, string>)}
     * - 如果指定了参数名,返回该参数的值
     * - 如果未指定参数名,返回所有的参数和值的KV对象
     */
    export declare const getUrlParams: (url: string, name?: string) => string | Record<string, string>;
  2. 普通的请求

    import { HttpRequest } from "hongfangze-http-request";
    
    const HttpRequestInstance = new HttpRequest(`https://localhost:443`);
    
    /**
         * 发送请求
         * @param {string} [url] 请求地址,请注意,无需包含baseUrl,默认“/”
         * @param {("GET" | "POST" | "PATCH" | "PUT" | "DELETE" | "HEAD" | "OPTIONS")} [method] 请求方式,默认“GET”
         * @param {Record<string, any>} [query] 地址栏请求参数,会用&key=value的方式连接
         * @param {Record<string, any>} [body] 请求包体,会以JSON的方式发送,WHATGW标准:GET及HEAD不支持该参数
         * @param {Record<string, string>} [headers] 额外的请求头部
         * @return {*}  {Promise<IHttpResponse>} 请求响应
         * @memberof httpRequest
         */
        send(url?: string, method?: "GET" | "POST" | "PATCH" | "PUT" | "DELETE" | "HEAD" | "OPTIONS", query?: Record<string, any>, body?: Record<string, any>, headers?: Record<string, string>): Promise<IHttpResponse>;
        /**
         * 下载文件
         * @param {string} [url] 请求地址,请注意,无需包含baseUrl,默认“/”
         * @param {string} [downloadFilename] 保存的文件全路径及文件名,不传则自动处理保存在当前目录下
         * @param {("GET" | "POST")} [method] 请求方式,默认“GET”
         * @param {Record<string, any>} [query] 地址栏请求参数,会用&key=value的方式连接
         * @param {Record<string, any>} [body] 请求包体,会以JSON的方式发送,WHATGW标准:GET及HEAD不支持该参数
         * @param {Record<string, string>} [headers] 额外的请求头部
         * @return {*}  {Promise<IHttpDownloadResponse>} 请求响应
         * @memberof httpRequest
         */
        download(url?: string, downloadFilename?: string, method?: "GET" | "POST", query?: Record<string, any>, body?: Record<string, any>, headers?: Record<string, string>): Promise<IHttpDownloadResponse>;
        /**
         * 上传文件
         * @param {string} [url] 请求地址,请注意,无需包含baseUrl,默认“/”
         * @param {("POST" | "PATCH" | "PUT")} [method] 请求方式,默认“POST”
         * @param {Record<string, any>} [query] 地址栏请求参数,会用&key=value的方式连接
         * @param {IFormData[]} [body] 请求包体,会以JSON的方式发送
         * @param {Record<string, string>} [headers] 额外的请求头部
         * @return {*}  {Promise<IHttpResponse>} 请求响应
         * @memberof httpRequest
         */
        upload(url?: string, method?: "POST" | "PATCH" | "PUT", query?: Record<string, any>, body?: IFormData[], headers?: Record<string, string>): Promise<IHttpResponse>;
        /**
         * 发送FormData数据
         * @param {string} [url] 请求地址,请注意,无需包含baseUrl,默认“/”
         * @param {("POST" | "PATCH" | "PUT")} [method] 请求方式,默认“GET”
         * @param {Record<string, any>} [query] 地址栏请求参数,会用&key=value的方式连接
         * @param {Record<string, string>} [body] 请求包体,会以FormData的方式发送
         * @param {Record<string, string>} [headers] 额外的请求头部
         * @return {*}  {Promise<IHttpResponse>} 请求响应
         * @memberof httpRequest
         */
        formdata(url?: string, method?: "POST" | "PATCH" | "PUT", query?: Record<string, any>, body?: Record<string, string>, headers?: Record<string, string>): Promise<IHttpResponse>;
    
  3. 原生的方式发送请求

    import { OriginalRequest } from "hongfangze-http-request";
    
    /**
     * 原生模式发送请求
     * @param {({
    *     protocol?: "http:" | "https:",
    *     url?: string,
    *     host?: string,
    *     port?: number,
    *     path?: string,
    *     method?: "get" | "delete" | "head" | "options" | "post" | "put" | "patch",
    *     headers?: Record<string, string>,
    * })} [options] 请求对象配置
    * - url 请求的全地址,如果传了该参数,会自动解析出来protocol、host、port、path,相对应的无需再传这一组参数
    * - protocol 协议,如果传了url,则该参数不需要,默认从url解析,解析不到就默认http
    * - host 请求域名或IP地址,如果传了url,则该参数不需要,默认从url解析,解析不到就默认localhost
    * - port 请求端口,如果传了url,则该参数不需要,默认从url解析,解析不到就默认80
    * - path 请求接口地址,如果传了url,则该参数不需要,默认从url解析,解析不到就默认/
    * - 注意:protocol、host、port、path的优先级大于url解析
    * - method 请求方式,默认get
    * - headers 请求头
    * @param {*} [data] 可选参数:请求数据包
    * - post/put/patch等可以传JSON等数据
    * - 如果是formData,请在headers中指定multipart/form-data,然后该参数传入FormData数据
    * @param {ServerResponse} [res] 可选参数:响应对象
    * - 可以将该次请求的响应结果通过pipe管道绑定到HTTP监听的响应对象中在,这样实现了自动响应,无需人为干预
    * @return {Promise<IHttpResponse | void>} 如果传了res参数,则没有任何返回值,直接通过管道进行响应;否则返回IHttpResponse对象,包含了请求响应的状态码、响应头、响应结果等信息
    */
    export declare const OriginalRequest: (options?: {
        protocol?: "http:" | "https:";
        url?: string;
        host?: string;
        port?: number;
        path?: string;
        method?: "get" | "delete" | "head" | "options" | "post" | "put" | "patch";
        headers?: Record<string, string>;
    }, data?: any, res?: ServerResponse) => Promise<void | import("./interface/httpResponse").IHttpResponse>;
  4. 代理API

    import { HttpProxy } from "hongfangze-http-request";
    
    const server = http.createServer((req, res) => {
        /*
        // 例如:百度搜索proxy
        var originUrl = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=proxy";
        var encodeUrl = encodeURIComponent(originUrl);
        // 使用代理后的地址:
        var proxyUrl = `http://localhost:3000?url=${encodeUrl}`;
        // http://localhost:3000?url=https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D8%26rsv_bp%3D1%26rsv_idx%3D1%26tn%3Dbaidu%26wd%3Dproxy
        */
        HttpProxy(req, res);
    }).listen(port, () => {
        console.log(`http proxy server start on port ${port}`);
    });
    
    /**
     * HTTP请求代理
     * - 作用:
     * - 1、隐藏真实IP地址,保护隐私
     * - 2、保护一些内网资源,避免内网资源全部暴露在公网中(比如有一个服务只在内网中,但其中一部分接口需要暴露到外网,那么就可以用这个来代理内网请求,并可适当的修改调用函数来实现接口代理白名单等)
     * - 传入request和response对象,自动转发请求后进行响应,接口传参url进行url编码
     * - 使用方式见test
     * @param {IncomingMessage} req 请求对象
     * @param {ServerResponse} res 响应对象
     * @return {*}  {Promise<void>}
     */
    export declare const HttpProxy: (req: IncomingMessage, res: ServerResponse) => Promise<void>;

版本迭代记录

2025-04-17 v1.0.1

  • 修复OriginalRequest的POST参数非JSON的传输问题。

2025-04-14 v1.0.0

  • 被移除后更名发布。

2025-03-24 v0.3.1

  • 修复原生请求,结果不正确返回的Bug。

2025-02-14 v0.3.0

  • 打包方式优化,支持注释的输出,方便用户调用。

2025-02-11 v0.2.1

  • 增加HTTP代理请求。
  • 增加一些请求相关函数。

2024-11-07 v0.1.1

  • 增加formdata,用于将body转换成formdata数据后发送。

2024-11-06 v0.1.0

  • 增加download,用于下载文件。
  • 增加upload,用于上载文件。
  • 优化send,正常返回status等数据。

2024-11-05 v0.0.2

  • 忽略HTTPS证书错误

2024-07-28 v0.0.1

  • 实现基础的请求函数封装
  • 后期将增加其他的封装