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
});
9
  • You can try Location header in response. Commented Jul 22, 2018 at 16:33
  • Need to change the order of those then send appropriate html file Commented Jul 22, 2018 at 16:37
  • @estus the spa runs on 127.0.0.1:3006 Commented Jul 22, 2018 at 16:41
  • I'd suggest to do this at web server level, Nginx or whatever you have. This isn't a very good use for app server, which Node is. Commented Jul 22, 2018 at 16:45
  • @estus thanks for response, but the problem here that i need authentification, it's so complicated Commented Jul 22, 2018 at 16:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

the problem is, that i get a blank page, i was redirected to my react application.
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?
all the page is rendred, but only html can be showed, that works with redirect, but not with proxy, i have a blank page.
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.
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('localhost:3000/surveys') ofcos in my project but everything seems fine
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.