0

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.

Kasia1
  • 23
  • 5
  • Have a look at [this answer](https://stackoverflow.com/a/52087529/1042409) to see if it helps – c-chavez Nov 15 '18 at 11:57
  • Check through the documentation listed [here](https://devcenter.heroku.com/articles/getting-started-with-nodejs) and follow [this](https://daveceddia.com/deploy-react-express-app-heroku/) tutorial to ensure that your app is setup correctly. Sounds like your react app isn't deployed. – elken Nov 15 '18 at 11:48

1 Answers1

0

In server.js your app object is not defined as in app = express()

elight
  • 562
  • 2
  • 17