Node.js 带缓存的nodejs节点http代理设置

Node.js 带缓存的nodejs节点http代理设置,node.js,caching,proxy,Node.js,Caching,Proxy,我正在尝试使用缓存设置节点http代理模块 . 我已经成功地配置了nodehttpproxy,以便在代理调用方面完成我需要做的事情,但是我想找到一种方法来缓存其中的一些调用 我当前的代码如下(省略了一些配置引导): 在handler函数中,我希望能够以某种方式捕获代理从“target”读取的内容,并将其流式传输到fs.createWriteStream(“/somepath”)中,然后再将其输出到res。然后,我将修改我的函数以查看以下内容: var handler = function(req

我正在尝试使用缓存设置节点http代理模块 . 我已经成功地配置了nodehttpproxy,以便在代理调用方面完成我需要做的事情,但是我想找到一种方法来缓存其中的一些调用

我当前的代码如下(省略了一些配置引导):

在handler函数中,我希望能够以某种方式捕获代理从“target”读取的内容,并将其流式传输到fs.createWriteStream(“/somepath”)中,然后再将其输出到res。然后,我将修改我的函数以查看以下内容:

var handler = function(req, res) {
    var path = '/somepath';
    fs.exists(path, function(exists) {
        if(exists) {
            console.log('is file');
            fs.createReadStream(path).pipe(res);
        } else {
            console.log('proxying');
            // Here I need to find a way to write into path
            proxy.web(req, res, {target: 'http://localhost:9000'});
        }
    });
};

有人知道怎么做吗?

这个问题的答案非常简单:

var handler = function(req, res, next) {

    var path = '/tmp/file';
    fs.exists(path, function(exists) {
        if(exists) {
            fs.createReadStream(path).pipe(res);
        } else {
            proxy.on('proxyRes', function(proxyRes, req, res) {
                proxyRes.pipe(fs.createWriteStream(path));
            });
            proxy.web(req, res, {target: 'http://localhost:9000'});         
        }
    });
};

正是我需要的!我为每个请求创建了唯一的路径,它似乎正在工作。
var handler = function(req, res, next) {

    var path = '/tmp/file';
    fs.exists(path, function(exists) {
        if(exists) {
            fs.createReadStream(path).pipe(res);
        } else {
            proxy.on('proxyRes', function(proxyRes, req, res) {
                proxyRes.pipe(fs.createWriteStream(path));
            });
            proxy.web(req, res, {target: 'http://localhost:9000'});         
        }
    });
};