title: Set up proxy server with multiple targets?
body: <p>I have 2 express servers:</p>
  
  <ol>
  <li><strong>api1</strong></li>
  <li><strong>api2</strong></li>
  </ol>
  
  <p>Both are accessed locally using:</p>
  
  <p><code>http://localhost:3000/news</code></p>
  
  <p><code>http://localhost:3001/stock</code></p>
  
  <p><strong><em>My aim:</em></strong></p>
  
  <p>Access both express servers endpoints from a Proxy server on <code>http://localhost:8008</code></p>
  
  <p><strong><em>My Issue:</em></strong></p>
  
  <p>I can only hit the first <strong>api1</strong>'s endpoints</p>
  
  <p>Any help would be greatly appreciated :)</p>
  
  <pre><code>const express = require('express');
  const { createProxyMiddleware } = require('http-proxy-middleware');
  
  const api1 = createProxyMiddleware({
    target: 'http://localhost:3000'
  });
  
  
  const api2 = createProxyMiddleware({
      target: 'http://localhost:3001'
    });
  
  const app = express();
  
  app.use(api1);
  app.use(api2);
  
  app.listen(8008);
  </code></pre>
  
  <p>**edit1:</p>
  
  <p>I tried this out, which works for <strong>api1</strong> endpoints but not <strong>api2</strong> endpoints.</p>
  
  <pre><code>const express = require('express');
  const { createProxyMiddleware } = require('http-proxy-middleware');
  
  const apiProxy = createProxyMiddleware('/', {
    target: 'http://localhost:3000'
  });
  const apiProxytwo = createProxyMiddleware('/', {
    target: 'http://localhost:3001'
  });
  
  const app = express();
  
  app.use(apiProxy,apiProxytwo);
  app.listen(8008);
  </code></pre>
  
  <p>Still looking for a solution!!@! Help please!</p>
  
  <p>Edit 2: Working solution</p>
  
  <pre><code>const express = require('express');
  const { createProxyMiddleware } = require('http-proxy-middleware');
  const app = express();
  
  
  app.use('/news/*', createProxyMiddleware({ target: 'http://localhost:3000', changeOrigin: true ,}));
  app.use('/stock/', createProxyMiddleware({ target: 'http://localhost:3001', changeOrigin: true, }));
  
  app.listen(8008);
  </code></pre>
  
  <p>Edit 3(*bonus): Applying solution in container setup - have to change localhost to container names(<strong><em>news</em></strong> and <strong><em>stock</em></strong>) for proxy to work with containers correctly.</p>
  
  <pre><code>const express = require('express');
  const { createProxyMiddleware } = require('http-proxy-middleware');
  const app = express();
  
  
  app.use('/news/*', createProxyMiddleware({ target: 'http://news:3000', changeOrigin: true ,}));
  app.use('/stock/', createProxyMiddleware({ target: 'http://stock:3001', changeOrigin: true, }));
  
  app.listen(8008);
  </code></pre>