test app
test app

Reputation: 1

React frontend, Node JS backend to hosting

How do I host a React JS application with a Node JS back end? Do I need to run the build command on the back end?

Upvotes: 0

Views: 394

Answers (2)

Y H R
Y H R

Reputation: 735

EDIT: Heroku no longer offers free server hosting as it used to

I suggest you use Heroku, you get to host your full stack application for free, directly from your GitHub repository, it automatically redeploys whenever you push something on your repo.

The only – slight – downside is having to wait ~5 seconds for the server to start up if your app hasn't been visited for a while and becomes idle (if you use a free option that is).

There are plenty of tutorials on how to do so.

As for serving the static version to your app in production — this could be of use:

server.js

/* If in production mode - serve compressed/static react content to server. i.e. what would be otherwise localhost:5000 would display frontend content.
/!\ Do not forget to generate Procfile and script for Heroku to insure proper generation of "build" directory /!\ */

if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "../frontend/build")));
  app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "../frontend", "build", "index.html"));
  });
}

Heroku will take care of it automatically if you tell your server to serve the static version with the code above.

There is also Glitch.com

Upvotes: 2

Azzam Michel
Azzam Michel

Reputation: 622

The way I do it: I build my react project and host it on the server. As for node js, I run it on it’s own, and use Pm2 to run it on the server ( https://pm2.keymetrics.io/docs/usage/quick-start/ ) but there’s plenty of other ways you can find on google.

I hope I answered your question

Upvotes: 1

Related Questions