2

I want to use node-http-proxy to forward requests only with certain content-type header. Since i want to filter requests, i can't just use forward option of proxy server. My idea is to use proxyReq event and if request satisfies conditions, forward it to another server. Can you give me example how can i do this?

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');
});
Kirill
  • 6,762
  • 4
  • 51
  • 81

0 Answers0