MERN не может получить / при развертывании в Heroku - PullRequest
0 голосов
/ 15 апреля 2020

Мое приложение работает на моем локальном сервере, но при развертывании на heroku я получаю следующее: введите описание изображения здесь

Я не могу найти ничего плохого в своем индексе. 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)); 

Я думаю, что проблема может быть в моем пакете. json file:

{
  "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 не отображаются какие-либо ошибки, и когда я ПУ sh Герою говорит, что сборка удалась.

1 Ответ

0 голосов
/ 16 апреля 2020

Проблема решена !!!

Была опечатка:

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') {

Я использовал. вместо _

...