@diginet/cors-proxy
TypeScript icon, indicating that this package has built-in type declarations

1.1.9 • Public • Published

CorsProxy

TypeScript class implementing a proxy server with CORS providing GET and POST access to servers without CORS.

Installation

npm install @diginet/cors-proxy --save

Usage

CLI

Create proxy server

import { CorsProxy } from "../CorsProxy"

const corsProxy = new CorsProxy("127.0.0.1", 8787, console.log)
/**
 * Constructor
 * @param host The interface (IP) to host the server
 * @param port Server port
 * @param log Optional log function with a string argument
 */
constructor(public host: string, public port: number, private log?: (message: string) => void)

Call from web client

Request

const url = "http://127.0.0.1:8787/get?url=" + encodeURIComponent("https://some.server/something")
request(url, (error, response, body) => {
    if (!error)
    	console.log(body)
    else
        console.log(error)
})

AJAX

var url = "https://some.server/something";
$.ajax({
        type: 'GET',
        accepts: 'application/json',
        url: "http://127.0.0.1:8787/get?url=" + encodeURIComponent("https://some.server/something"),
        contentType: 'application/json',
        error: function (jqXHR, textStatus, errorThrown) {
            alert('error');
        },
        success: function (result) {
            console.log(result);
        }
    });

Using Postman

To encode the url query parameter correctly, select the URL parameter content, right-click and select EncodeURIComponent.

Readme

Keywords

none

Package Sidebar

Install

npm i @diginet/cors-proxy

Weekly Downloads

0

Version

1.1.9

License

ISC

Unpacked Size

15.2 kB

Total Files

17

Last publish

Collaborators

  • anderswestberg
  • erikwe
  • viktorw