我一直在尝试用Heroku进行MEAN应用程序,但是我遇到了这个错误。我不知道这是什么问题...我需要一些帮助。我寻找许多解决方案。但是我有同样的错误。 而且我还附上了我的git。 https://github.com/handsomehyunsu/resume/tree/heroku 谢谢。
2020-11-08T08:59:22.685557+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=hyunsukimresume.herokuapp.com request_id=51fc6ab7-2c9d-431c-b8a2-2a7a82ce79c3 fwd="14.50.155.250" dyno= connect= service= status=503 bytes= protocol=https
2020-11-08T08:59:23.143224+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=hyunsukimresume.herokuapp.com request_id=b9cb9676-fd30-4987-9a48-3bf71fa095a3 fwd="14.50.155.250" dyno= connect= service= status=503 bytes= protocol=https
Procfile
web: node server.js
package.json
{
"name": "my-resume",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"dev": "nodemon server.js",
"postinstall": "ng build --output-path dist"
},
"private": true,
"dependencies": {
"@angular/animations": "^10.1.6",
"@angular/cdk": "^10.2.7",
"@angular/common": "~10.1.5",
"@angular/compiler": "~10.1.5",
"@angular/core": "~10.1.5",
"@angular/forms": "~10.1.5",
"@angular/material": "^10.2.7",
"@angular/platform-browser": "~10.1.5",
"@angular/platform-browser-dynamic": "~10.1.5",
"@angular/router": "~10.1.5",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"bootstrap": "^4.5.3",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.11",
"mongoose-unique-validator": "^2.0.3",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1001.6",
"@angular/cli": "~10.1.6",
"@angular/compiler-cli": "~10.1.5",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"nodemon": "^2.0.6",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.0.2"
},
"engines": {
"node": "~10.16.3",
"npm": "~6.14.8"
}
}
server.js
const { debug } = require('console');
const http = require('http');
const app = require('./backend/app');
const normalizePort = val => {
var port = parseInt(val, 10);
if(isNaN(port)){
//named pipe
return val;
}
if(port >= 0){
//port number
return port;
}
return false;
};
const onError = error => {
if(error.svscall !== "listen"){
throw error;
}
const bind = typeof addr === "string" ? "pipe " + addr : "port " + port;
switch(error.code){
case "EACCES":
console.log(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.log(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
const onListening = () => {
const addr = server.address();
const bind = typeof addr === "string" ? "pipe " + addr : "port " + port;
debug("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
const server = http.createServer(app);
server.on("error", onError);
server.on("listening", onListening);
server.listen(port);
backend / app.js
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require("mongoose");
const postsRoutes = require('./routes/posts');
const userRoutes = require('./routes/user');
const app = express();
//mongodb connect
mongoose.connect("mongodb+srv://test_username:" + process.env.MONGO_ATLAS_PW + "@cluster0.utmlm.mongodb.net/myResume?retryWrites=true&w=majority")
.then(() => {
console.log('connected to database!!');
})
.catch(() => {
console.log('connection failed');
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//CORS
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});
app.use("/posts", postsRoutes);
app.use("/user", userRoutes);
module.exports = app;
答案 0 :(得分:0)
Heroku H10-App崩溃可能是由于很多原因,所以我会尽力列出它们,您可以尝试检查它们。
如果您的服务器位于server.js中,并且您的Procfile指向app.js,则肯定会使您的应用程序崩溃
将PORT设置为Heroku环境变量将使您的应用程序崩溃。 Heroku自动设置一个可通过process.env.PORT访问的端口。自己设置端口会使您的应用程序崩溃。
尝试编辑server.js文件
只需const server = http.createServer(process.env.PORT || 3000);
来启动服务器,请跳过normalizePort
如果不能解决问题,则可能是您缺少必需的环境变量。
package.json
文件的引擎部分中设置节点版本。
像这样例如:
{
"name": "myapp",
"description": "a really cool app",
"version": "1.0.0",
"engines": {
"node": "12.11.1",
"npm": "6.14.8"
}
}