【问题标题】:MERN Cannot Get / when deploying to Heroku部署到 Heroku 时 MERN 无法获取 /
【发布时间】:2020-07-27 21:29:22
【问题描述】:

我的应用程序在我的本地服务器上运行,但是当我部署到 heroku 时,我得到了这个: enter image description here

我在 index.js 中找不到任何错误:

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

require('dotenv').config();

//mongoose 
mongoose.connect(process.env.URI || 'mongodb://localhost/book_trak', {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
  .catch(err => console.log(err));

var db = mongoose.connection;
db.on('error', (err) => console.log(err));
db.once('open', () => console.log('we are connected'));

const app = express();

//body parser no longer needed 
app.use(express.urlencoded({ extended: false }))
app.use(express.json())

app.use(cors())
app.use('/book', require('./routes/book'));
app.use('/author', require('./routes/author'));

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


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

}

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

app.listen(PORT, () => console.log('running on Port ' + PORT)); 

我认为问题可能出在我的 package.json 文件中:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "engines": {
    "node": "12.16.0"
  },
  "main": "index.js",
  "scripts": {
    "client-install": "cd front_end && npm install",
    "start": "node index",
    "server": "nodemon index",
    "client": "cd front_end && npm start",
    "dev": "concurrently \" npm run server  \" \" npm run client\"",
    "heroku-postbuild": "cd front_end && npm install && npm run build"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.9.7"
   },
  "devDependencies": {
    "concurrently": "^5.1.0",
    "nodemon": "^2.0.2"
  }
}

出于某种奇怪的原因,我的 heroku 日志没有显示任何错误,当我推送到 heroku 时,它说构建成功。

【问题讨论】:

  • 嗨,朋友,这不妥当,但我需要帮助。我在 Heroku 上的应用程序,但它只显示后端。鉴于您发布的文件(您是否从 travesy MERN 教程中学习?),我的应用程序看起来很像您的应用程序?无论如何,您是否使用 path 来解决这个问题?除了 index.js 文件中的内容之外,我还需要使用 path 的其他方式吗?

标签: heroku deployment package.json favicon mern


【解决方案1】:

问题解决了!!!

有一个错字:

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

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

}

应该是:

if(process.env.NODE_ENV === 'production') {

我用过。而不是 _

【讨论】:

    【解决方案2】:
    try this it may help you!!
    
    if(process.env.NODE_ENV == "production"){
        app.use(express.static("./client/build"));
        const path = require("path");
        app.get("*", (req, res) => {`enter code here`
            res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
    
        })
    }
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2021-03-30
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2020-10-22
    • 2020-05-12
    相关资源
    最近更新 更多