![](/img/trans.png)
[英]Setting up node-http-proxy for Django/NodeJS app on heroku
[英]nodejs node-http-proxy setup with cache
我正在嘗試使用緩存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);
});
在處理程序函數內部,我希望能夠以某種方式捕獲代理從“目標”讀取的內容並將其流式傳輸到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'});
}
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.