Node.js的缓存代理服务器(Node.js caching proxy server)

2019-09-22 15:22发布

我想了解如何使用Node.js,在那里我可以在我的本地磁盘上转发到任何网页和缓存他们创造一个HTTP缓存代理服务器!

下面是我的第一次尝试代码:

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 + "/");

我想用我的应用程序suchas任何缓存库:节点静态(https://github.com/cloudhead/node-static),静态缓存,....

如果网站,我走访工作正常,我的应用程序会转发给它。 如果不是我的应用程序将获得并返回我,我的磁盘上的缓存数据。

是有这个工程的任何溶液?

谢谢 !

文章来源: Node.js caching proxy server