Giter Club home page Giter Club logo

node-global-proxy's Introduction

Node Global Proxy

Global Proxy for Node.JS with Friendly APIs.

Work with Axios, Request, Got, Superagent etc.

Install

npm install node-global-proxy --save

Introduction

Basic use with Javascript

const proxy = require("node-global-proxy").default;

proxy.setConfig("localhost:1080");
proxy.start();

/** Proxy working now! */

proxy.stop();

/** Proxy stop working now! */

Also, you can use a config object

const proxy = require("node-global-proxy").default;

proxy.setConfig({
  http: "http://localhost:1080",
  https: "https://localhost:1080",
});
proxy.start();

/** Proxy working now! */

proxy.stop();

/** Proxy stop working now! */

Use with Typescript or ES6+

This package is develop by typescript and fully support typescript, you can use it as follow:

import proxy from "node-global-rpoxy";

proxy.setConfig("http://localhost:1080");
proxy.start();

API

  • proxy.start() Start using global proxy
  • proxy.stop() Stop using global proxy
  • proxy.setConfig(config : string | NodeGlobalProxyConfig) Set config global proxy by url string or config object
  • proxy.getConfig() Get current config object
  • proxy.system() Get system config and set as node global proxy

Examples

This is an example with a proxy server run on localhost:10809, and get ip information from system proxy, no proxy and user config proxy.

const axios = require('axios');
const proxy = require('node-global-proxy').default;

let PROXY_URL = "localhost:10809"

const test = async () => {
    console.log(proxy);

    proxy.system();
    console.log(proxy.getConfig());
    res = await axios.get("http://ip-api.com/json", {
        timeout: 3000
    });
    console.log(res.data);

    proxy.setConfig(PROXY_URL);
    console.log(proxy.getConfig());

    proxy.start();
    res = await axios.get("http://ip-api.com/json", {
        timeout: 3000
    });
    console.log(res.data);

    proxy.stop();
    res = await axios.get("http://ip-api.com/json", {
        timeout: 3000
    });
    console.log(res.data);

    proxy.setConfig({
        http: "http://" + PROXY_URL,
        https: "https://" + PROXY_URL
    });
    console.log(proxy.getConfig());
    proxy.start();
    res = await axios.get("http://ip-api.com/json", {
        timeout: 3000
    });
    console.log(res.data);
}

test();

node-global-proxy's People

Contributors

huankong233 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.