Deploying ReactJS application in production (with nodeJS backend)

前端 未结 3 764
清酒与你
清酒与你 2021-02-11 05:28

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 ove

3条回答
  •  遥遥无期
    2021-02-11 06:02

    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!

提交回复
热议问题