简体   繁体   中英

How to deploy reactJS + nodeJS app on Heroku?

I have problem with deploying my app to heroku. I have output of react app in built folder (path: /client/build/index.html)

server.js

const express = require('express');
const path = require('path');

const cors = require('cors');
const passport = require('passport');

app.use(express.static(path.resolve(__dirname + '/client/build')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
const port = process.env.PORT || 5000;

const bodyParser = require('body-parser');
const mongoose = require('mongoose');

app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.use(cors())
mongoose.connect(process.env.MONGODB_URI)
    .then(() => console.log("success"))
    .catch(err => console.log(err))
app.use(passport.initialize());
require('./config/passport')(passport);


const server = app.listen(port,  function(err) {
  if (err) {
    return;
  } 
  console.log('server listening on port: %s', port);
});

Procfile

web: node server.js

package.json (server) {

  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "server": "nodemon server.js",
    "client-install": "npm install --prefix client",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run client\" \"npm run server\""
  },
  "author": "",
  "license": "ISC",
 "dependencies": {
    "concurrently": "^4.0.1",
    "express": "^4.16.4",
    "jsonwebtoken": "^8.3.0",
    "mongoose": "^5.3.4",
    "node": "^8.10.0",
    "nodemon": "^1.18.4",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "react-scripts": "1.0.11",
  },
  "devDependencies": {
    "nodemon": "^1.18.4"
  }
}

Is it ok to show my reactApp from build folder? How can I deploy my server? Is it enough to put command in Procfile or should I add for example "heroku-postbuild" in package.json? Thanks for help

PS. After pushing to heroku I got this index.html file, but blank page and server is not working.

server.js您的app对象未定义为app = express()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM