Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

node.js - Cannot GET / MERN stack deployment on heroku

Hi i am having trouble when trying to deploy my mern stack to heroku. every way i have tried to deploy it i get the same error of get the error Cannot GET /. i have gone through the tutorials but still no luck

this is my server. js file

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");

const users = require("./routes/api/users");
const payees = require("./routes/api/payees");
const transactions = require("./routes/api/transactions");

const path = require('path')

const app = express();

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

// DB Config
const db = require("./config/keys").mongoURI;

// Connect to MongoDB
mongoose
  .connect(
    db,
    { useNewUrlParser: true }
  )
  .then(() => console.log("MongoDB successfully connected"))
  .catch(err => console.log(err));


app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(bodyParser.json());



// Passport middleware
app.use(passport.initialize());

// Passport config
require("./config/passport")(passport);

// Routes
app.use("/api/users", users);
app.use("/api/payees", payees);
app.use("/api/transactions", transactions)

if(process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, 'client', 'build')));

  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
  });

}

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server up and running on port ${port} !`));

module.exports = { app };

and this is my package.json file

{
  "name": "STUBANK",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "client-install": "npm install --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently "npm run server" "npm run client"",
    "test": "mocha",
    "heroku-postbuild": "cd client && npm install && npm run build"
  },
  "author": "Ojini Jude",
  "license": "MIT",
  "dependencies": {
    "@material-ui/core": "^4.11.2",
    "axios": "^0.21.1",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "chai": "^4.2.0",
    "chai-http": "^4.3.0",
    "classnames": "^2.2.6",
    "crypto-random-string": "^3.3.0",
    "dotenv": "^6.2.0",
    "express": "^4.16.4",
    "is-empty": "^1.2.0",
    "jsonwebtoken": "^8.5.1",
    "jwt-decode": "^3.1.2",
    "mdbreact": "^5.0.1",
    "mocha": "^8.2.1",
    "mongoose": "^5.3.6",
    "particles-bg": "^2.5.0",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "password-validator": "^5.1.1",
    "react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.1",
    "react-dropdown": "^1.9.0",
    "react-redux": "^7.2.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^4.0.1",
    "react-search-box": "^2.0.2",
    "react-search-field": "^1.2.0",
    "react-select": "^3.1.1",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "supertest": "^6.0.1",
    "validator": "^13.5.1"
  },
  "devDependencies": {
    "concurrently": "^4.1.2",
    "nodemon": "^1.19.4"
  },
  "engines": {
    "node": "12.16.0"
  }
}

Any help would be greatly appreciated.

file structure

question from:https://stackoverflow.com/questions/65860078/cannot-get-mern-stack-deployment-on-heroku

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...