[英]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. 学校没有内部服务器(代理,电子邮件或文件服务器),我有兴趣在自己的Macbook Pro上实施自己的服务器,以改善学生的学习体验。
I have already set up a node
(ecstatic) webserver that I use to share my files and notes. 我已经设置了一个node
(静态)Web服务器,用于共享文件和注释。 I think the next step it a proxy server. 我认为下一步是代理服务器。
I would like to set up a proxy server to: 我想将代理服务器设置为:
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 例如,所有学生可能都在使用bbc bitesize http://www.bbc.co.uk/education并同时使用同一页面,因此从理论上讲缓存将极大地帮助您
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. 我正在使用javascript和node进行游戏,因此,如果这在node上是可行的,那将会很棒,如果不是,我愿意使用python,java或其他任何东西。 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. 我想让学生连接到我机器上的myIP:port并像浏览网站一样浏览。
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) 或者(但不太可取),我可以让他们在自己的Internet设置中将计算机添加为代理,然后以这种方式运行服务器(任何建议)
Thanks for any help, my students will appreciate it. 感谢您的帮助,我的学生将不胜感激。
Paul 保罗
Here are some things I have tried: 这是我尝试过的一些方法:
I have tried node-http-proxy 我已经尝试过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 具有NodeJ的简单代理服务器
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. 这适用于xkcd.com,但不适用于www.bbc.co.uk,我希望Cookie可能是问题所在,而且由于许多链接都是绝对的,因此我需要拦截它们并将其替换为相对于代理的链接。
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. Node.js缓存代理服务器这看起来很有希望,但是我正在寻找一个更完善的解决方案,因为这有点麻烦。
You can use node-proxy-cache
: 您可以使用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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.