3

I have 2 express servers:

  1. api1
  2. api2

Both are accessed locally using:

http://localhost:3000/news

http://localhost:3001/stock

My aim:

Access both express servers endpoints from a Proxy server on http://localhost:8008

My Issue:

I can only hit the first api1's endpoints

Any help would be greatly appreciated :)

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);

**edit1:

I tried this out, which works for api1 endpoints but not api2 endpoints.

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);

Still looking for a solution!!@! Help please!

Edit 2: Working solution

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);

Edit 3(*bonus): Applying solution in container setup - have to change localhost to container names(news and stock) for proxy to work with containers correctly.

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);
cgd
  • 57
  • 5
  • 1
    You should add a path for each api. Currently, you are not adding a path and it must be adding both api to root path `/` which is why one api is accessible and the other is not. Try: `app.use('/api1', api1); ` and `app.use('/api2', api2);`. – Abrar Hossain May 10 '20 at 18:48
  • hello, I made changes as suggested. Still not working – cgd May 10 '20 at 18:53
  • 1
    So what happens when you send a request to `http://localhost:8008/api1` and `http://localhost:8008/api2`? Which one is accessible and which one is not? – Abrar Hossain May 10 '20 at 18:55
  • I get ```Cannot GET /api2``` when I try `http://localhost:8008/api2/stock` I get ```Cannot GET /api2/stock``` – cgd May 10 '20 at 19:18
  • same applies to ```api1``` – cgd May 10 '20 at 19:19
  • I did this, which gets the first api endpoints to work, but not the second ``` const express = require('express'); const { createProxyMiddleware } = require('http-proxy-middleware'); const apiProxy = createProxyMiddleware('/', { target: 'http://localhost:3001' }); const apiProxytwo = createProxyMiddleware('/', { target: 'http://localhost:3000' }); const app = express(); app.use(apiProxy,apiProxytwo); app.listen(8008); ``` – cgd May 10 '20 at 19:43

1 Answers1

2

proxy.js:

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);

newsServer.js

const express = require('express');
const app = express();

app.get('/news/headlines', (req, res) => {
    res.send('hello from headlines')
});

app.get('/news/other', (req, res) => {
    res.send('hello from other')
})


app.listen(3000);

query http://localhost:8008/news/headlines => 'hello from headlines'

stockServer.js

const express = require('express');
const app = express();

app.get('/stock', function(req, res) {
    res.send('hello from api2');
  });


app.listen(3001);

query http://localhost:8008/stock => 'hello from api2'

max
  • 449
  • 2
  • 9
  • Hi maxi, thank you for your response. I am trying to avoid doing it like that as I will have to edit 400+ endpoints manually all the ***news*** and ***stock*** endpoints have a second parts to them for example ```/news/headlines``` any thoughts? – cgd May 10 '20 at 23:00
  • previously execute `node newsServer.js` and `node stockServer.js` – max May 10 '20 at 23:08
  • thank you, I couldn't get your proxy to work 100% So I ended up changing all the endpoint names for one of the express servers. const express = require('express'); const { createProxyMiddleware } = require('http-proxy-middleware'); const apiProxy = createProxyMiddleware('/api/v1', { target: 'http://localhost:3000', logLevel: 'debug' }); const apiProxytwo = createProxyMiddleware('/', { target: 'http://localhost:3001', logLevel: 'debug', }); const app = express(); app.use(apiProxy,apiProxytwo); app.listen(8008); Thank you for your help Maxi! – cgd May 11 '20 at 01:04
  • 1
    @cgd it's a shame, you just had to change from `'/news'` to `'/news/*'` in proxy.js to be able to scale your routes – max May 11 '20 at 01:15
  • Hi Maxi - gave it go - and it works! A thousand thank you's :) – cgd May 11 '20 at 12:17
  • 1
    @cgd very good! Could you accept my answer as your solution please? – max May 11 '20 at 13:05