Deploy a React app + Node server with Heroku

戏子无情 提交于 2020-01-16 08:08:12

问题


I want to deploy a project (React app + Node server), but I'm new to deployment,

I wanted to know : do I need to have the React app in a Github repo and the Node server in another, or I can deploy all in one ?

Currently, I have 1 Github repository with a folder "frontend" and an other "backend",

I want to have my React app on -> nameofmyapp.herokuapp.com and the Node server on -> api-nameofmyapp.herokuapp.com,

If someone got ideas... Thanks


回答1:


Anything is possible, you just need to understand how things are working... my advice would be that you start simple and have a single repo that contains front+back, you can then deploy that as a single heroku app.

One app can only have a unique heroku url, so you cannot have what you mention nameofmyapp + api-nameofmyapp hosted by a single heroku instance, this would need to be hosted by two different instances, which means code from two repos.

Usually for a node app, you would create an /api route that is hosted by the same app, so you have your frontend served at nameofmyapp.herokuapp.com and your api at nameofmyapp.herokuapp.com/api with some sub routes, for example nameofmyapp.herokuapp.com/api/items.

You should be able to easily find tons of node/react/heroku tutorials on the web, just play a bit with it to experiment and build some understanding of how those are working together.

Hope that helps.




回答2:


While in theory that's not a problem, I would suggest maybe considering keeping things on one domian for reasons such as additional latency and connection trouble as well as path issues such as you are facing. It would seem to me that you would ideally just like to prefix the name of your app with 'backend' or similar and in such a case I would just consider setting up a sub domain on a domain which I had control ie mydomain.com and backend.mydomain.com. While developing on Heroku this model could prove to be tricky as each 'site' or app is separate and not actually intended to work together while they most certainly could. Consider setting up separate routes and an endpoint for 'backend' on your app, similar to your frontend login, then when you are finished developing your app and happy you could register your domain name and point it to your app and point a subdomain ie backend.mysite.com or login.mysite.com to your endpoint on Heroku ie mysite.com/backend. Unless you have a specific reason for separating them into their own repos with separate source control and urls, it might make debugging things much harder. Apologies if I missed your point. Most web hosting companies should allow you to register a subdomain or vanity domian free or charge because you own the primary domian. Just some considerations.



来源:https://stackoverflow.com/questions/59520440/deploy-a-react-app-node-server-with-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!