部署到Heroku时,MERN无法获取/

时间:2020-04-15 01:18:20

标签: heroku deployment package.json favicon mern

我的应用程序可以在本地服务器上运行,但是当我部署到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时,它表示构建成功。

1 个答案:

答案 0 :(得分: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') {

我用过。而不是_

相关问题