无法在Heroku上托管MEAN堆栈应用程序

时间:2019-09-18 06:28:41

标签: node.js angular heroku deployment

我正在尝试部署应用程序,但问题是在我的根文件夹中,我有两个不同的文件夹,一个用于后端,一个用于前端。 首先,我尝试按原样进行推送,只是将数据库从本地更改为云。 推送成功,但是无法打开该应用程序。得到了以下错误 An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command 接下来,合并两个文件夹,并对路由进行一些必要的更改(遵循不同的教程)。现在我什至不能收支平衡。 尝试了很多方法,然后遵循并看了不同的教程,阅读指南和博客,但无法托管我的应用程序。 如果您能帮助我,我将非常感谢您 注意:My project is working fine in localhost 这是我的package.json来自前端文件夹

{
  "name": "office-space",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot -prod",
    "start":"node server.js"
  },
  "private": true,
  "dependencies": {
    "@angular/cli": "~8.1.0",
    "@angular/compiler-cli": "~8.1.0",
    "@angular/animations": "~8.1.0",
    "@angular/common": "~8.1.0",
    "@angular/compiler": "~8.1.0",
    "@angular/core": "~8.1.0",
    "@angular/forms": "~8.1.0",
    "@angular/platform-browser": "~8.1.0",
    "@angular/platform-browser-dynamic": "~8.1.0",
    "@angular/router": "~8.1.0",
    "angular-font-awesome": "^3.1.2",
    "bootstrap": "^4.3.1",
    "font-awesome": "^4.7.0",
    "jquery": "^3.4.1",
    "material-design-icons": "^3.0.1",
    "ngx-pagination": "^4.1.0",
    "nonblockjs": "^1.0.8",
    "pnotify": "^4.0.0",
    "popper.js": "^1.15.0",
    "rxjs": "~6.4.0",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.801.0",
     "@angular/language-service": "~8.1.0",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.3.30",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "engines": {
    "node":"12.3.1",
    "npm":"6.11.1"
  }

}

这是我的后端文件夹中的package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mongoose": "^5.6.10"
  }
}

最后是我的服务器文件

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    mongoose = require('mongoose'),
    //DB = 'mongodb://localhost:27017/OfficeSpace'
    DB = 'mongodb+srv://omersjd:omersjdp@officespace-szwno.mongodb.net/OfficeSpace?retryWrites=true&w=majority'



const employeeRoute = require('./routes/employee.route');
//mongoose.Promise = global.Promise;
mongoose.connect(DB, { useNewUrlParser: true }, (err, response) => {
    if (err) {
        console.log('Error connecting to Database: ', err)
    }
    else {
        console.log('Connected to ' + DB);
    }
});
const app = express();

app.use(express.static(__dirname + "/dist"))
app.get('*', function (req, res) {
    res.sendFile(path.join(__dirname + "/dist/OfficeSpace/index.html"))
})
app.use(bodyParser.json());
app.use(cors());
app.use('/', employeeRoute);

let port = process.env.PORT || 4000;
//console.log(employeeRoute);

const server = app.listen(port, function () {
    console.log('Listening on port ' + port);
})

这是我的目录结构:

├── api (backend folder) 
└── frontend-app contents

1 个答案:

答案 0 :(得分:0)

您是否检查了错误日志?您可以通过

heroku logs --tail

该错误应为mongodb连接检查并尝试解决...

希望这对您有帮助...