我想使用node-http-proxy
仅使用特定内容类型标头转发请求。由于我想过滤请求,我不能只使用代理服务器的forward
选项。
我的想法是使用proxyReq
事件,如果请求满足条件,则将其转发给另一台服务器。你能举例说明我该怎么办?
var http = require('http'),
fs = require('fs'),
connect = require('connect'),
httpProxy = require('http-proxy'),
proxy = httpProxy.createProxyServer({});
proxy.on('proxyReq', function (proxyReq, req, res, options) {
if (req.headers['content-type']==='text/xml') {
// send it to target and also forward to another server
}
});
var app = connect()
.use(function (req, res) {
proxy.web(req, res,
{
target: {
port: 8081,
host: 'localhost'
}
})
});
http.createServer(app).listen(8080, function () {
console.log('proxy listen 8080');
});