Paul Paul - 1 month ago 12
Javascript Question

How can I set up a web cache-proxy server in nodejs to reduce internet usage

I am a teacher at a school in China with very limited internet bandwidth. The school has no internal servers (proxy, email or file server) and I am interested in implementing my own on my macbook pro in order to improve the students' learning experience.

I have already set up a

node
(ecstatic) webserver that I use to share my files and notes. I think the next step it a proxy server.

I would like to set up a proxy server to:


  1. reduce bandwidth usage

  2. improve speed for students and

  3. potentially allow access to sites by piggybacking on my VPN.



For example all the students might be using bbc bitesize http://www.bbc.co.uk/education and using the same page at the same time so caching would in theory help immensely

I am playing with javascript and node so if this is possible in node that would be great, if not I am willing to use python, java or whatever. Ideally a solution would work out of the box but leave room for me to improve and tweak it as my skills develop.

I would like to students to connect to myIP:port on my machine and browse as if they were on the website.

Alternatively (but less preferably) I could get them to add my computer as a proxy in their internet settings and run a server that way (any suggestions)

Thanks for any help, my students will appreciate it.

Paul

Here are some things I have tried:

I have tried node-http-proxy

httpProxy.createProxyServer({target:'http://www.bbc.co.uk/'}).listen(9000);


but it wasn't allowing me to browse (just dumping me on the page not found page) and I am unsure if it would cache effectively.

Simple Proxy Server with NodeJs

var app = express();
app.use('/', function(req, res) {
var url = 'https://xkcd.com/' + req.url;
var options = {
url: url,
rejectUnauthorized: false
}
req.pipe(request(url)).pipe(res);
});
app.listen(8001, function(){console.log("listen on 8001");})


This works for xkcd.com but not for www.bbc.co.uk, I expect cookies may be the problem, also as many of the links are absolute I would need to intercept them and replace them with links relative to the proxy.

Node.js caching proxy server
This looks promising but I am looking for a more finished solution as this is a bit over my head.

Answer

You can use node-proxy-cache:

var ProxyCache = require( 'node-proxy-cache' ),
    proxyCache = new ProxyCache({}),
    DS = {};

proxyCache.when( /google/, {
    getKey: function( path, query ) { // allows you to generate keys 
        return 'foo:' + path; 
    },
    headers: {
        'X-Bar': 'Baz' // set custom headers when sending to proxy 
    },
    caching: false // don't cache responses from google 
});

proxyCache.when( /foo.org/, {
    cacheTime: function( cacheEntry, req, proxyRes ) {
        if ( cacheEntry.body.length > 10000000 ) {
            return -1; // don't cache big responses 
        }

        if ( req.url.match( /bar/) ) {
            return 0; // cache bar stuff forever 
        }

        if ( proxyRes.statusCode === 404 ) {
            return -1; // don't cache 404 responses 
        }

        return 10000; // only cache for 10 seconds 
    }  
});

proxyCache.store({ // custom storeAdapter 
    get: function( key, callback ) {
        callback( null, DS[key] ); // getting information 
    },
    set: function( key, value, callback ) { // setting values to store 
        DS[ key ] = value; // value.body is a buffer  
        callback( );
    }
});

proxyCache.listen( 9000 );