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 🙏

© 2024 – Pkg Stats / Ryan Hefner

http-clientp

v1.0.38

Published

Simplified HTTP/HTTPS/PROXY request client.

Downloads

27

Readme

http-clientp

  • 支持基于http、https、proxy的请求
  • 支持回调函数附带数据
  • 支持设置超时时间
  • 支持使用函数返回代理
  • 具体用法见test/test.js
  • 运行test请先安装mocha
  • 运行test请启动本地8888端口的代理,fiddler或者charles或者其他代理都可以

httpcontext模板格式:

{
	"request":{
		"proxy":{"host":"127.0.0.1","port":8888},//设置请求代理,共两种设置方式,任选其一即可
		"http_proxy":"http://127.0.0.1:8888",    //设置请求代理,共两种设置方式,任选其一即可
		"headers":{},		
		"encode":true,   //是否对url进行encodeURI操作,默认为true
		"timeout":"1000", //请求超时
		"postdata":"a=1&b=1"    //post请求参数,
		 options: { 
                    method: 'GET',
                    path: 'https://www.npmjs.com/package/http-clientp' 
                   }
	},
	"response":{
		"charset":"",//返回编码
		"statusCode":200
	},
	"data":{             //附带数据
	    "key":"value",
	    "key1":"value1"
	}
}

  • 基础用法
var options = {
    "method":"GET",
    "path":"https://www.baidu.com/"
};
httpClient.request(options, function (err, body, res, httpContext) {
    console.log(res.statusCode)
});
****GET
var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'url': 'https://www.baidu.com/'}, null, {'host': '127.0.0.1', 'port': 8888}, 1000);
httpClient.request(httpcontext.request.options, function (err, body, res, httpContext) {
    console.log(res.statusCode);
    console.log(httpContext.data.key);
    console.log(httpContext.data.url);
}, httpcontext);
****POST
var httpcontext = httpClient.build_httpcontext('POST', {
    'key': 'value',
    'url': test_url
}, 'a=1&b=2&c=3', {'host': '127.0.0.1', 'port': 8888}, 1000);
httpClient.request(httpcontext.request.options, function (err, body, res, httpContext) {
    console.log(res.statusCode);
    console.log(httpContext.data.key);
    console.log(httpContext.data.url);
}, httpcontext);
  • get请求
 httpClient.get('https://www.baidu.com/', function (err, body, res, httpContext) {
    console.log(res.statusCode)
 });
  • 超时请求
var httpcontext = {'request': {'timeout': 1000}};
httpClient.get(test_url, function (err, body, res, httpContext) {
    console.log(res.statusCode)
}, httpcontext);
  • 代理请求
var httpcontext = {'request': {'proxy': {'host': '127.0.0.1', 'port': 8888}}};
httpClient.get(test_url, function (err, body, res, httpContext) {
    console.log(res.statusCode)
}, httpcontext);
  • 代理请求,http_proxy方式
var httpcontext = {'request': {'http_proxy': 'http://127.0.0.1:8888'}};
httpClient.get(test_url, function (err, body, res, httpContext) {
    console.log(res.statusCode)
}, httpcontext);
  • 代理请求,http_proxy方式,支持proxy auth
var httpcontext = {'request': {'http_proxy': 'http://username:[email protected]:8888'}};
httpClient.get(test_url, function (err, body, res, httpContext) {
    console.log(res.statusCode)
}, httpcontext);
  • 将请求的数据附带到结果中
var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'city':'北京'});
httpClient.get(test_url, function (err, body, res, httpContext) {
    console.log(res.statusCode)
    console.log(httpContext.data.key)
    console.log(httpContext.data.city)
}, httpcontext);
  • 根据同步函数设置代理
var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'url': test_url});
httpcontext.proxymodel = 'dynamic';
httpcontext.callback = function (httpcontext) {
    console.log(httpcontext.res.statusCode)
};
httpClient.request_select_proxy(httpcontext, function (callback) {
    var http_proxy = syncProxy();//根据函数获取代理,代理是动态获取的时候可以用此种方法
    httpcontext.request.http_proxy = http_proxy;
    callback(httpcontext);
});
function syncProxy() {
    return '127.0.0.1:8888';
}
  • 根据异步函数设置代理
var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'url': test_url});
httpcontext.proxymodel = 'dynamic';
httpcontext.callback = function (httpcontext) {
    console.log(httpcontext.res.statusCode)
};
httpClient.request_select_proxy(httpcontext, function (callback) {
    asyncProxy(function (http_proxy) {
        httpcontext.request.http_proxy = http_proxy;
        callback(httpcontext);
    })
});
function asyncProxy(calback) {
    calback(proxy_url);
}
  • 设置头
var httpContext = {'request': {'headers': {"Cookie": "a=a"}}};
httpClient.get({'path': test_url, 'headers': {"Cookie": "b=b"}}, function (err, body, res, httpContext) {
    console.log(httpContext.request.headers.Cookie)
    console.log(res.statusCode)
}, httpContext);
var httpClient = require('../lib/http-clientp');
var fs = require('fs');
var httpcontext = {
    'request': {
        'http_proxy': 'http://127.0.0.1:8888',
        'headers': {'Content-Type': 'image/jpeg;charset=UTF-8', 'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8'}
    }, 'response': {'charset': 'buffer'}
};
httpClient.get('http://202.109.191.178:8081/wt-web/captcha?0.4344325280246917', function (err, body, res, httpContext) {
    fs.writeFileSync("/Users/jgm/Downloads/chrome/captcha.png", body);
}, httpcontext);