我构建了我的第一个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
答案 0 :(得分:2)
Heroku documentation表示您需要将端口绑定到PORT
。因此,您可能希望将代码更改为
app.set('port', process.env.PORT || 4040);