在 Heroku 上部署 MEAN 应用程序时出现应用程序错误
Posted
技术标签:
【中文标题】在 Heroku 上部署 MEAN 应用程序时出现应用程序错误【英文标题】:Application error on Deploying MEAN app on Heroku 【发布时间】:2018-05-30 05:42:14 【问题描述】:我构建了我的第一个 MEAN 应用程序并想部署它。它在我的系统中运行良好,但是当我在 Heroku 上部署它时出现应用程序错误。我运行 npm start 检查 Heroku 控制台中的任何错误,但是控制台记录了“服务器启动”成功消息,这意味着节点服务器工作正常。
app.js
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const mongoose = require('mongoose');
const mongoStore = require('connect-mongo')(session);
const path = require('path');
const app = express();
const index = require('./routes/index');
//using 'session'
app.use(session(
secret: 'Xiomi',
resave: true,
saveUninitialized: true,
store: new mongoStore( mongooseConnection: mongoose.connection )
));
//use static files
app.use(express.static(path.join(__dirname, 'public')));
// use 'body-parser' in app
app.use(bodyParser.urlencoded(extended:true));
app.use(bodyParser.json());
//making api accessable
app.use(function(req,res,next)
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Methods","GET, POST, PUT")
res.header("Access-Control-Allow-Headers","Origin, X-Requested-
With,Content-Type,Authorization,Accept");
next();
);
// use 'index' route
app.use('/api',index);
app.get('*', (req, res) =>
res.sendFile(path.join(__dirname, '/public/index.html'));
);
// Handle '404'
app.use((req,res,next) =>
const error = new Error('Not Found');
error.status = 404;
next(error);
);
// Error Handler
app.use((err,req,res,next) =>
res.json(
message:err.message,
err
);
);
module.exports = app;
start.js
//dot env includes
require('dotenv').config(path:'info.env');
//mongoose connection setup
const mongoose = require('mongoose');
mongoose.connect(process.env.DB,useMongoClient:true);
mongoose.Promise = global.Promise; //use Es6 promises
mongoose.connection.on('error',err =>
console.log("Mongoose connection failed");
);
//require models
require('./models/question');
require('./models/answer');
require('./models/user');
const app = require('./server');
app.set('port',process.env.port||4040);
app.listen(app.get('port'),()=>
console.log("Server started");
);
这是我的应用程序的 heroku 日志:
2017-12-16T02:26:07.368109+00:00 heroku[run.2613]: Starting process with command `npm start`
2017-12-16T02:26:07.625833+00:00 heroku[run.2613]: State changed from starting to up
2017-12-16T02:26:15.275993+00:00 heroku[run.2613]: Client connection closed. Sending SIGHUP to all processes
2017-12-16T02:26:15.809932+00:00 heroku[run.2613]: State changed from up to complete
2017-12-16T02:26:15.796854+00:00 heroku[run.2613]: Process exited with status 10
2017-12-16T02:26:21.198223+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=qa-application-mean.herokuapp.com request_id=5afdf0d7-af4f-47d1-954b-5e0141f116b8 fwd="117.205.140.102" dyno= connect= service= status=503 bytes= protocol=https
2017-12-16T02:26:23.974360+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=qa-application-mean.herokuapp.com request_id=a6b2524d-8412-45c4-9f0f-5d4fcd15417e fwd="117.205.140.102" dyno= connect= service= status=503 bytes= protocol=https
2017-12-16T02:26:46.596768+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api/questions" host=qa-application-mean.herokuapp.com request_id=a8859faa-c2fb-4c88-b295-da472c7b7ecc fwd="117.205.140.102" dyno= connect= service= status=503 bytes= protocol=https
2017-12-16T02:26:48.979172+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=qa-application-mean.herokuapp.com request_id=f721e237-b4c8-465c-8073-6ab5d93f5160 fwd="117.205.140.102" dyno= connect= service= status=503 bytes= protocol=https
【问题讨论】:
【参考方案1】:Heroku documentation 表示您需要将端口绑定到PORT
。因此,您可能希望将代码更改为
app.set('port', process.env.PORT || 4040);
【讨论】:
以上是关于在 Heroku 上部署 MEAN 应用程序时出现应用程序错误的主要内容,如果未能解决你的问题,请参考以下文章
在 Heroku 上部署 React 应用程序和 Nginx 时出现问题
在 Heroku 上部署时出现 Django 1.7 迁移错误
如何解决在 Heroku 上部署 React 应用程序时出现的错误
在 heroku 上部署反应应用程序时出现问题 - 控制台上的错误代码 503