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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mm_https

v1.6.5

Published

超级美眉HTTP请求模块 - 支持自动Cookie管理、代理设置、文件下载等功能

Readme

mm_https

这是一个基于Node.js的HTTP请求帮助类模块,支持自动管理cookie、模拟登录、代理设置等功能。

安装

npm install mm_https

基本用法

初始化

const {
    Http
} = require('mm_https');

// 基本初始化
const http = new Http();

// 带配置初始化
const httpWithConfig = new Http({
    headers: {
        'User-Agent': 'Custom User Agent'
    },
    cookie: 'session=123456',
    proxy: 'http://127.0.0.1:10809'  // 设置代理
});

配置选项

  • headers: 自定义请求头,可以是对象或字符串格式
  • cookie: 初始cookie字符串
  • proxy: 代理服务器地址
  • rejectUnauthorized: SSL证书验证选项,默认false

核心功能

GET请求

const {
    Http
} = new Http();

const http = new Http();

// 基本GET请求
async function simpleGet() {
    const res = await http.get('https://api.example.com/data');
    $.log.debug(res.body);  // 响应内容
}

// 带请求头的GET请求
async function getWithHeaders() {
    const headers = {
        'Authorization': 'Bearer token123'
    };
    const res = await http.get('https://api.example.com/protected', headers);
    $.log.debug(res.body);
}

// 带cookie的GET请求
async function getWithCookie() {
    const res = await http.get('https://api.example.com/user', null, 'session=abc123');
    $.log.debug(res.body);
}

POST请求

const {
    Http
} = new Http();

const http = new Http();

// JSON格式POST请求
async function postJson() {
    const data = {
        username: 'test',
        password: '123456'
    };
    const res = await http.post('https://api.example.com/login', data);
    $.log.debug(res.body);
}

// 表单格式POST请求
async function postForm() {
    const formData = {
        name: 'John',
        age: 25
    };
    const res = await http.post('https://api.example.com/submit', formData, null, 'form');
    $.log.debug(res.body);
}

// 自定义请求头和cookie的POST请求
async function postWithHeadersAndCookie() {
    const data = { id: 123 };
    const headers = { 'X-Custom-Header': 'value' };
    const res = await http.post('https://api.example.com/api', data, headers, 'json', 'session=xyz789');
    $.log.debug(res.body);
}

文件下载

const {
    Http
} = new Http();

const http = new Http();

// 基本文件下载
async function downloadFile() {
    const url = 'https://example.com/image.jpg';
    const filename = './downloads/image.jpg';
    const file = await http.download(url, filename);
    $.log.debug('下载文件保存在:', file);
}

// 自动识别文件类型下载
async function autoDownload() {
    const url = 'https://example.com/image.jpg';
    const filename = './downloads/image';  // 无需指定扩展名
    const file = await http.download(url, filename, true);
    $.log.debug('下载文件保存在:', file);
}

// 带请求头的下载
async function downloadWithHeaders() {
    const url = 'https://example.com/protected/file.pdf';
    const filename = './downloads/document.pdf';
    const headers = {
        'Authorization': 'Bearer token123'
    };
    const file = await http.download(url, filename, false, headers);
    $.log.debug('下载文件保存在:', file);
}

Cookie管理

const {
    Http
} = new Http();

const http = new Http();

// 设置Cookie
http.setCookie('session=abc123; user=john');

// 或者通过对象设置多个Cookie
http.setCookie({
    'session': 'abc123',
    'user': 'john'
});

// 获取当前所有Cookie
const cookieString = http.getCookie();
$.log.debug('当前Cookie:', cookieString);

// 清除所有Cookie
http.clear();

高级用法

代理设置

// 使用代理服务器
const http = new Http({
    proxy: 'http://127.0.0.1:10809'
});

// 访问需要代理的网站
async function accessWithProxy() {
    const res = await http.get('https://google.com');
    $.log.debug(res.body);
}

自动重定向处理

模块会自动处理301、302等重定向响应,无需额外配置。

自定义请求

// 使用req方法发起自定义请求
async function customRequest() {
    const options = {
        method: 'POST',
        href: 'https://api.example.com/data',
        body: { key: 'value' },
        headers: { 'X-Custom': 'value' },
        type: 'json',
        cookie: 'session=123'
    };
    
    const res = await http.req(options);
    $.log.debug(res.body);
}

响应对象

所有请求方法返回的响应对象包含以下字段:

  • status: 状态码和消息
  • headers: 响应头
  • body: 响应内容(字符串)
  • buffer: 原始响应数据(Buffer对象,适用于二进制数据)

错误处理

async function handleErrors() {
    try {
        const res = await http.get('https://api.example.com/notfound');
        $.log.debug(res.body);
    } catch (err) {
        $.log.error('请求失败:', err.message);
    }
}

注意事项

  1. 所有请求方法都返回Promise,需要使用async/await或.then()处理
  2. 默认已配置常用的请求头,可以通过配置覆盖
  3. 支持自动解码gzip、deflate等压缩响应
  4. 支持GB2312和GBK编码的响应自动转换

许可证

ISC License