0

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?

1
  • Did you try just doing nodejs hosting in a google search.? Commented Oct 8, 2021 at 20:25

2 Answers 2

2

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

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

Comments

1

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

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.