Hi, Im planning to build a backend in Node and a Frontend on React. I know those are two things who are working together. But is it wise to build the frontend and serve those pages from the same Node server as the backend?
Or, do you run a Node server which serves the api only, and a second Node server serving the HTML only. Of course they run on separate ports and how do you handle that? So, what is the common way to setup a backend and a frontend? Everything on one Node stack seems complex and confusing to me. What is your advice on this?
One way I’ve seen it done is to have a single Node server that serves both the API, and also the HTML of the front-end application. When the URL being requested is https://yourdomain/api/*, then the server routes the request to an API controller. When the URL being requested is anything else, for example just https://yourdomain, then the server serves up the HTML for the front-end application.
But this doesn’t mean that you need to have your front-end code and back-end code in the same repository/project. Your back-end project just needs to contain the build output of your front-end project so that it can serve it up. The complexity is that when you’re ready to deploy your server, you’ll need a way to update it so that it has the latest version of your front-end application. This could be just manually copying the build output into your server project, or writing a script to do that, or maybe eventually using a build system like Jenkins which automates the whole process.