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

simple-proxies

v1.1.0

Published

A small nodejs module to use proxies in an application

Downloads

209

Readme

This module provides a simple API for using HTTP proxies in a NodeJS application.

For example, when you make many HTTP requests (specially in the SEO world ;-)), you need to make them through a group of proxies. The goal of this package is to simplify your code for managing a list of proxies : load proxies, get ramdomly one proxy, check proxies, ...

Install

npm install simple-proxies

##Structure of the txt proxy file

Each line of the file should respect the one of following structure :

  • host:port:username:password
  • host:port
  • comment with #
  • blank line

Using proxies in your own app

There are 2 components :

  • ProxyFileLoader : load proxies from a txt file & store them into a JS object.
  • ProxyChecker : check if proxies are valid & not banned by Google.

An example with Axios

import * as proxyLoader from '../lib/proxyfileloader.js'
import axios from 'axios'

// Change the config if you want to use a specific txt file
// This is not necessary if you plan to use proxies.txt on the current folder
const config = proxyLoader.config().setProxyFile('yourfile.txt')
const proxyList = await proxyLoader.loadProxyFile(config)

// Make a request with one proxy
const response = await axios({ uri: 'http://www.xxx.com', proxy: proxyList.getProxy().getConnectionParams() })

API

Load the default proxy file

import * as proxyLoader from 'simple-proxies/lib/proxyfileloader'
const proxyList = await proxyLoader.loadDefaultProxies();
const proxies = proxyList.getProxies();

By default, the list of proxies are in a file called proxies.txt and it should be in the root folder of your project.

Read proxies from a specific txt file

import * as proxyLoader from 'simple-proxies/lib/proxyfileloader'
var config = proxies
  .config()
  .setProxyFile('filename.txt')
  .setCheckProxies(false);

const proxyList = await proxyLoader.loadProxyFile(config)

The config can be used to personalize how the proxies have to be managed. See FileConfig in model.js to get all options.

The object model

After loading the proxies from a file or from a db, you receive a ProxyList object which contains an array of Proxy objects.

Get all Proxies

var proxies = proxyList.getProxies()
proxies.map(proxy => console.log(proxy)) // return an array of proxies : {host, port, valid, userName, password}

Get one proxy

var proxy = proxyList.getProxy()

console.log(proxy.getUrl()) //return the complete URL of the proxy : http://username:pwd@host:port
console.log(proxy.userName)
console.log(proxy.password)
console.log(proxy.host)
console.log(proxy.port)
console.log(proxy.valid)

This method loops on the proxy list on each call.

Get one proxy randomly

var proxy = proxyList.pick()

console.log(proxy.getUrl()) //return the complete URL of the proxy : http://username:pwd@host:port
console.log(proxy.userName)
console.log(proxy.password)
console.log(proxy.host)
console.log(proxy.port)
console.log(proxy.valid)

See the model.js to get a complete overview of the API provided by this model.

Check the proxies

Anywhere in your code or in a cron, you can check if the proxies are still valid by using the proxy checker. This component uses a ProxyList to make the check.

import * as checher from 'simple-proxies/lib/proxychecker'
import * as proxyLoader from 'simple-proxies/lib/proxyfileloader'

const proxyList =  await proxyLoader.loadDefaultProxies();
var config = checker.config().setGoogleAdress('http://www.google.be');
await checker.checkAllProxies(proxyList, config, function(error) {
//the proxies in proxyList should be updated (valid or not valid)

The config can be used to personalize how the proxies has to be checked. See CheckConfig in model.js to get all options.