Tom*_*ski 3 proxy caching node.js
我正在尝试使用缓存node-http-proxy模块设置 node-http-proxy模块.我已经设法配置node-http-proxy来完成我在代理调用方面需要做的事情,但我想找到一种方法来缓存其中一些调用.
我当前的代码如下(省略一些配置bootstrapping):
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);
});
Run Code Online (Sandbox Code Playgroud)
在处理程序函数内部,我希望能够以某种方式捕获代理从"目标"读取的内容并将其流式传输到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'});
}
});
};
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点 ?
这个问题的答案最终非常简单:
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'});
}
});
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2116 次 |
最近记录: |