2

I am new in node and react, and I am trying to do subdomain routing in my MERN application. I have found out that I cant route my subdomains in react, so that leaves me with node. In node, there are many ways to it, like vhost, or by

app.get('*',(req,res)=>{
 res.sendFile(path.resolve(__dirname,'client','build','index.html'))
}) 

But my question is, do I have to make different apps per subdomain? for e.g.

  • for admin.application.com -> admin app in react
  • for otherdomain.application.com-> other app in react
  • for just application.com -> one more app in react.

Is this the only way to do it? If yes, can someone with more experience tell me more about this way? If no, please guide.

1
  • I just created this monster, its an express app with a subdomain express and an automatic build for the public folders with react the creation was intended for using dashboard.example.com with the costumer services UI and the client side of the webapp github.com/sabasm/node-express-react-and-reactSubdomain hope it helps someone out! Apr 20, 2019 at 22:43

2 Answers 2

4

Each subdomain similar to a domain needs to have its own DNS record, so it is redirected to an IP address. You can direct the traffic from different subdomains to the same server and handle them by the same appliation by modifying your DNS records.

If there is no disctinction between these subdomains, you can just use the same Node.js application to process all the incoming traffic. But if you want to mount the traffic from each subdomain to a different port or path (e.g., admin.application.com to /admin of your application and otherdomain.application.com to /other path). In this case, you can use a proxy application such as nginx or HAProxy.

My suggestion is to develop different applications for each subdomain if their business logic is different. Otherwise, you can include everything in the same application.

2
  • Thanks for the quick response, if I go with the "otherwise" and only write one backend app, do I need to write different react.js applications for each subdomain or is there any way I can have only 1 react.js app for all subdomains?
    – Deepanshu
    Oct 2, 2018 at 8:08
  • @Deepanshu, not necessarily. There are workarounds to use the same react.js app with the traffic from each subdomain directed to a specifc path of the app, but it would make much more sense to use a separate react.js app for each subdomain. Again, just to reiterate, they can all live on the same server and share resources, but it will make your life much easier if you have a separate app for each subdomain and just use a reverse proxy server (e.g., nginx or haproxy) to route the traffic from each subdomain to the proper app.
    – Behrooz
    Oct 3, 2018 at 22:26
1

You can use:

req.get('host') to get the hostname. Based on the hostname, you can make further decisions about routing the request.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.