Search code examples
node.jstypescriptproxyhttp-proxyhttp-proxy-middleware

How to proxy to many different targets using http-proxy-middleware?


In the http-proxy-middleware library, the documentation states that you can use the target option to specify where you would like to proxy the request. However, they also allow you to use the router option to specify a function that will be used to functionally resolve the target at runtime.

Docs: https://www.npmjs.com/package/http-proxy-middleware

I am using TypeScript and if I look at the declaration file for the proxy I can see this:

proxy

You can see here that both router and target are nullable. My assumption would be that if you use one, the other can be omitted, but you always need at least 1.

However, if I use the router property like this:

app.use("/pipe", proxy({
    changeOrigin: true,
    router: (req: IIncomingMessageWithCookies) => {
        return "https://www.google.com";
    }
}));

And omit the target, then at runtime I get this error:

> node ./dist/app.js

C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\lib\config-factory.js:43
    throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING)
    ^

Error: [HPM] Missing "target" option. Example: {target: "http://www.example.org"}
    at Object.createConfig (C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\lib\config-factory.js:43:11)
    at new HttpProxyMiddleware (C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\lib\index.js:17:30)
    at module.exports (C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\index.js:4:10)
    at Object.<anonymous> (C:\SkyKick\SkyKick.SEWR\src\dist\app.js:8:18)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)

I realized that I can place pretty much anything into target and it will run just fine and my router function is the thing that actually defines the target proxy. Is this just a bug with the library or am I misunderstanding what these 2 options are used for?


Solution

  • Include both the target and the router property. The router property is there to re-target option.target for specific requests.

    import express = require('express');
    import proxy = require('http-proxy-middleware');
    
    const app = express();
    
    app.use('/api', proxy({
        target: 'http://www.github.com',
        changeOrigin: true,
        router: function (req: IncomingMessage) {
            return 'http://www.stackoverflow.com'
        }
    }));
    
    app.listen(3000);
    

    The target property is optional in the Config type because it is allowed to be empty when we use the shorthand like this:

    app.use('/api', proxy('http://www.github.com',
        {
            changeOrigin: true,
            router: function (req: IncomingMessage) {
                return 'http://www.stackoverflow.com'
            }
        }
    ));