部署到Heroku时为什么会出现“应用程序错误”? (MERN应用)

时间:2020-05-20 02:40:24

标签: heroku

我已经检查了日志,这就是它的意思: enter image description here

这是我的app.js文件(在根级别):

// Importing frameworks, libraries, routes, and JSON parser 
const express = require('express');
const mongoose = require('mongoose');
const rewards = require('./routes/api/rewards');
const bodyParser = require('body-parser');
const path = require('path');

// Loading static build folder for production
if (process.env.NODE_ENV === 'production') {
  app.use(express.static('frontend/build'));
  app.get('/', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
  })
}

// Create a new Express server
const app = express();

// Setup middleware for body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Import MongoDB key
const db = require('./config/keys').mongoURI;

// Connect to MongoDB using Mongoose
mongoose
  .connect(db, {useNewUrlParser: true, useUnifiedTopology: true})
  .then(() => console.log('Connected to MongoDB successfully'))
  .catch(err => console.log(err));

// Create basic express routing
app.use('/api/rewards', rewards);

// Determining which port to run on
// Deploying on Heroku requires the server to be on process.env.PORT
const port = process.env.PORT || 5000;  

// Start a socket and listen for connections on the path
// Display a success message when server is running correctly
app.listen(port, () => console.log(`Server is running on port ${port}`));

此错误消息确切说明了什么指针/提示?我最好的猜测是它与我的一条路线有关,但我不太确定...

1 个答案:

答案 0 :(得分:0)

您正在将您的应用变量设置为低于生产量检查以提供反应构建。

请移动行

const app = express();

如果您的支票上方

正确的app.js将是:

// Importing frameworks, libraries, routes, and JSON parser 
const express = require('express');
const mongoose = require('mongoose');
const rewards = require('./routes/api/rewards');
const bodyParser = require('body-parser');
const path = require('path');

// Create a new Express server
const app = express();

// Loading static build folder for production
if (process.env.NODE_ENV === 'production') {
  app.use(express.static('frontend/build'));
  app.get('/', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
  })
}   

// Setup middleware for body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Import MongoDB key
const db = require('./config/keys').mongoURI;

// Connect to MongoDB using Mongoose
mongoose
  .connect(db, {useNewUrlParser: true, useUnifiedTopology: true})
  .then(() => console.log('Connected to MongoDB successfully'))
  .catch(err => console.log(err));

// Create basic express routing
app.use('/api/rewards', rewards);

// Determining which port to run on
// Deploying on Heroku requires the server to be on process.env.PORT
const port = process.env.PORT || 5000;  

// Start a socket and listen for connections on the path
// Display a success message when server is running correctly
app.listen(port, () => console.log(`Server is running on port ${port}`));