Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including xhr2proxy with all npm packages installed. Try it out:

var xhr2proxy = require("xhr2proxy")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

xhr2proxy v0.1.4

HTTP/HTTPS proxy middleware of connect/express for XHR(XMLHTTPRequest) with referer restriction and appending Access-Control-Allow-Origin for cross domain(CORS) XHR2

##Description

HTTP/HTTPS proxy plugin of connect/express for XHR(XMLHTTPRequest) with referer restriction and appending Access-Control-Allow-Origin for cross domain XHR2 Based on cdproxy by macchadogj(thanks)

##Installation

$ npm install xhr2proxy

##Usage

###basic

var xhr2proxy = require('xhr2proxy');

connect()
    .use(xhr2proxy({prefix:'myprefix'})) //proxy by default.
    .use(function ( req, res ) {
    res.end("Hello from Connect!");
}).listen(8000);

//var xhr=new XMLHttpRequest();
//xhr.open('get','http://localhost:8000/myprefix/http://myremote.server',false);
//xhr.send();

##basic(express)

var xhr2proxy = require('xhr2proxy');
var app=require('express').createServer();
app.use(xhr2proxy());
app.listen(8000);

//var xhr=new XMLHttpRequest();
//xhr.open('get','http://localhost:8000/prefix/http://myremote.server',false);
//xhr.send();

###Appending Access-Control-Allow-Origin to response headers

It allows XMLHttpRequest level2 to connect with non supported servers from outside of the domains which runs this script.

var xhr2proxy = require('xhr2proxy');

connect()
    .use(xhr2proxy({prefix:'myprefix',acao:'*'}) //Append [Access-Control-Allow-Origin: *]
    .use(function ( req, res ) {
    res.end("Hello from Connect!");
}).listen(8000);

//var xhr=new XMLHttpRequest();
//xhr.open('get','http://anywhrere:8000/myprefix/http://myremote.server',false);
//xhr.send();

###referer restriction

prevent out-site using by referer restriction.CAUTION:It cannot recognize fake headers.

var xhr2proxy = require('xhr2proxy');

connect()
    .use(xhr2proxy({prefix:'myprefix',referer:/^http:\/\/localhost/})) //deny if referer isnot http://localhost...
    .use(function ( req, res ) {
    res.end("Hello from Connect!");
}).listen(8000);

//var xhr=new XMLHttpRequest();
//xhr.open(''get',http://localhost:8000/myprefix/http://myremote.server',false);
//xhr.send();
//with referer http://localhost:8000/anywhere.html

###other options

suppressHostNameOnRequest(bool)

default or false:
    GET http://localhost/path HTTP/1.0
                .
                .

true:
    GET /path HTTP/1.0
                .
                .

##Running unit tests

$npm test

##History

0.1.0: Adds option suppressHostNameOnRequest.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free