Deploying ReactJS application in production (with nodeJS backend)

14,321

Solution 1

I assumed you used create-react-app to generate your react project. My folder structure is like:

  • client (it is my react app)
  • config
  • controllers
  • routes
  • models
  • index.js (express app is in index.js)
  • package.json

Following step that I used to deployed my app to Heroku:

  1. In package.json, added this line to the scripts "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client".

  2. Then added this "engines": { "node" : "[your node version]" } after scripts.

  3. In index.js, put the following code after your routes set up

    if (process.env.NODE_ENV === "production") {
      app.use(express.static("client/build"));
      const path = require("path");
      app.get("*", (req, res) => {
        res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
      });
    }
    
  4. I assume that you use git for version control and already install heroku. Open your terminal, then heroku login -> heroku create -> git push heroku master. If you do not get any error, you are success to deploy your app.

This is my GitHub https://github.com/dnp1204/instagrom that I deployed my app to Heroku. I hope it will give you some idea how to do so

Hope you will get it to work!

Solution 2

To deploy a react app build with the cli create-react-app , first you need to set the host in your package.json , so in your package.json added this line

"homepage":"http://localhost/" ,assuming you will host your app on the root directory of your webserver 

your package.json will look something like this

{
  "name": "nameofyourapp",
  "version": "0.1.0",
  "private": true,
  "homepage":"http://localhost/",
  "dependencies": {
    .....
  },
  "scripts": {
  ......
  }
}

then inside your app folder run this commande

npm run build

then copy what inside your build folder and paste it in the root directory of your serveur you will ended up with files like this

httdocs //root directory of your webserver
  |
  |-static
  |
  |-index.html
  |-serviceworker.js

after that you cant access your webserver from your browser : http://localhost/

Share:
14,321

Related videos on Youtube

Mich
Author by

Mich

Updated on June 04, 2022

Comments

  • Mich
    Mich almost 2 years

    our project is now over, we only have two weeks to give back the project for our final year's studies at University. Our teacher told us that now that development phase was over we would have to go deploy it in production phase.

    We used ReactJS for the frontend (hosted in localhost://3000) and NodeJS for server/database management (hosted in localhost://3004).

    I've done some research so far on how to deploy it (teacher said that ideally it was a zip that we can insert into a webpage, just like a html one, and that it would work).

    So far I haven't managed to do that. Much people recommand using "webpack" for managing this but we didn't configure it in the beginning because we had no clue about reactJS and I don't know if we can do it right now. I've read tutorials etc and tried some things with NODE_ENV but till now I'm stuck with errors.

    Can anyone help me & my teammates ?

    Thanks in advance!

    • Abslen Char
      Abslen Char about 6 years
      and what is your question so far ?
    • Mich
      Mich about 6 years
      how to deploy the website =/
    • Abslen Char
      Abslen Char about 6 years
      did you use react-creat-app ? can you post your webpack.config.js and your folder structure
    • Mich
      Mich about 6 years
      I think so.. my folders are "build, node_modules, public, src" and the packages jsons. Weve started the project two or three months ago but yeah I think we used this command
    • Abslen Char
      Abslen Char about 6 years
      ok , i will answer your question in the answer field
    • Mich
      Mich about 6 years
      yeaaaah thing is our webpack config dissapeared cause someone deleted it, I've tried rebuilding it but without success okay thank you man, i'm going to try what you propose :D
  • Mich
    Mich about 6 years
    nevermind !!!! I just understood that I had to copy all of this into the wamp folder "www"
  • Mich
    Mich about 6 years
    hey man, i'm coming back to you, I've a question.. My server is hosted 3004 but my client is in 3000. our teacher advised us that it should've been in the same port but it didn't work so we used 2 ports.. can you tell me how to launch my server at 3004 at the same time ?
  • Abslen Char
    Abslen Char about 6 years
    once you serve it on an appache server you can configure your port in the apache config file and serve it on what ever port you want the port is configured on the server it has nothing to do with react
  • Mich
    Mich about 6 years
    thanks for the fast answer, my teacher told us that apache worked for another group but we chose to go with nodejs and wamp for the server.. is there a way around? I have zero knowledge about apache :/
  • Abslen Char
    Abslen Char about 6 years
    so if i understund you correctly you need to use node js instead of apache right ?
  • Mich
    Mich about 6 years
    exactly man :/ i'm trying dnp's approach with heroku atm since i'm abit confused. but yeah i want to use nodejs instead of apache
  • Abslen Char
    Abslen Char about 6 years
    you need to setup a nodejs server blog.risingstack.com/your-first-node-js-http-server
  • Mich
    Mich about 6 years
    yeah I have a server.js which is around 400 lines, but it uses the port 3004 while my react client uses 3000 (i can't put both on the same port cause it says "port is already used").. so its a bit awkward i don't know how to launch my nodejs and react at same time..
  • Abslen Char
    Abslen Char about 6 years
    I am afraid you need to open a new question for this , we cant keep posting comment here , please consider accepting this answer and open new one for NodeJS
  • Mich
    Mich about 6 years
    Alright man thanks for all your help ill do that tomorrow ! Have a great day