【发布时间】:2014-11-09 10:15:11
【问题描述】:
我正在尝试使用缓存设置 node-http-proxy 模块 node-http-proxy module。我已经设法将 node-http-proxy 配置为在代理调用方面做我需要做的事情,但我想找到一种方法来缓存其中一些调用。
我目前的代码如下(省略了一些配置引导):
var http = require('http');
var https = require('https');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
var fs = require('fs');
var handler = function(req, res) {
proxy.web(req, res, {target: 'http://localhost:9000'});
};
var server = http.createServer(handler).listen(config.children.http.port, function() {
console.log('Listening on port %d', server.address().port);
});
var secure = https.createServer(config.children.https.options, handler).listen(config.children.https.port, function() {
console.log('Listening on port %d', secure.address().port);
});
在处理函数内部,我希望能够以某种方式捕获代理从“目标”读取的内容,并将其流式传输到 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'});
}
});
};
有人知道怎么做吗?
【问题讨论】: