Node.js caching proxy server

188 views
Skip to first unread message

thuan le minh

unread,
Sep 20, 2012, 12:43:52 AM9/20/12
to nod...@googlegroups.com

I am trying to create a http caching proxy server using node.js , where i could forward to any webpages and cached them on my local disk !

The following is my first attempt code :

var http = require('http'),
url  
= require('url'),
sys  
= require('url');

var fs = require('fs');

var port = "9010";

// function notFound
function notFound(response){
response
.writeHead(404, "text/plain");
response
.end("404 : File not Found");
}


//create simple http server with browser requet and browser response
http
.createServer(function(b_request, b_response){

//Parse the browser request'url
var b_url = url.parse(b_request.url, true);
if(!b_url.query || !b_url.query.url) return notFound(b_response);

//Read and parse url parameter (/?url=p_url)
var p_url = url.parse(b_url.query.url);

//Initialize Http client
var p_client = http.createClient(p_url.port || 80, p_url.hostname);

//Send request
var p_request = p_client.request('GET', p_url.pathname || "/", {
   host
: p_url.hostname
});
p_request
.end();

//Listen for response
p_request
.addListener('response', function(p_response){
   
//Pass through headers
    b_response
.writeHead(p_response.statusCode, p_response.headers);
   
//Pass through data
     p_response
.addListener('data', function(chunk){
            b_response
.write(chunk);
   
});

   
//End request
    p_response
.addListener('end', function(){
        b_response
.end();
   
});
});
}).listen(port);

console
.log("Server running at http://127.0.0.1:" +port + "/");

i want to use any cached library for my app suchas : Node-static(https://github.com/cloudhead/node-static), Static cache, ....

if website that i visited is working fine , my app will forward to it . If not my app will get and return me data that cached on my disk .

is there any solutions for this works ?

thank !

Reply all
Reply to author
Forward
0 new messages