0

I have two different react js Application, and i want to redirect the user to the application depending on the url

example :

app.get('/' , (req,res,next) => {
  // redirect to first SPA
});

app.get('/admin' , (req,res,next) => {
  // redirect to another SPA
});
Skander Ben Khelil
  • 115
  • 1
  • 3
  • 8

2 Answers2

3

If the react app is to be served from the node.js server, simply send each app's index.html:

app.get('/admin', (req, res) => {
  res.sendFile('admin_app/index.html');
});

app.get('/', (req, res) => {
  res.sendFile('public_app/index.html');
});

If those files are served from a different process on the same machine you will have to proxy the requests:

const httpProxy = require('http-proxy');
const proxy = httpProxy.createServer({});

app.get('/admin', (req, res) => {
  proxy.web(req, res, { target: 'http://localhost:3006/admin' });
});

app.get('/', (req, res) => {
  proxy.web(req, res, { target: 'http://localhost:3006/' });
});

Redirects would also work, as long as the process at 127.0.0.1:3006 listens on all public addresses. But a redirect includes that the users browser navigates to a different URL. E.g. if your node.js server was running at example.com (i.e. port 80, which the browser omits) and redirects to the other process, the browser would now show example.com:3006. By proxying the request, the URL will stay on example.com.

Lucas S.
  • 2,303
  • 1
  • 14
  • 20
  • the problem is, that i get a blank page, i was redirected to my react application. – Skander Ben Khelil Jul 22 '18 at 17:20
  • Is it actually blank, i.e. no content sent, or just a white page because nothing can be rendered (e.g. due to js files not being loaded)? Did you check the console in your browsers developer tools for errors? Do you get any content when loading the URL with `curl`? – Lucas S. Jul 22 '18 at 17:23
  • all the page is rendred, but only html can be showed, that works with redirect, but not with proxy, i have a blank page. – Skander Ben Khelil Jul 22 '18 at 17:27
  • If all static content is hosted at `localhost:3006` you will to redirect/proxy more that just those two paths. My answer was mostly about giving an idea how it can be done. What/How much has to be redirected depends a lot on your specific set up and on the applications. – Lucas S. Jul 22 '18 at 17:30
  • Hey @LucasS. well i am able to redirect to localhost:3000 without using proxy so exactly what is use of proxy i just did res.redirect('http://localhost:3000/surveys') ofcos in my project but everything seems fine – Ratnabh kumar rai Sep 10 '19 at 20:19
  • @Ratnabhkumarrai I explained that briefly in the last paragraph. Both approaches will work fine. The only difference is which port the browser is sending requests to. In the case of redirects, the browser sends requests to ports other than `80` (or `443` for HTTPS). The browser's address bar will then show these ports (e.g. `http://example.com:3000/surveys`). Users are not used to this, so I wouldn't recommend it for any public-facing system. When proxying the requests, the port doesn't change (`http://example.com/surveys`). – Lucas S. Sep 10 '19 at 21:03
0

How about in a route middleware, res.redirect('http://someOtherServer/?

Such as res.redirect('http://localhost:5000').

Maybe this other answer will help Nodejs - Redirect url

jboxxx
  • 707
  • 10
  • 19