I build my first MEAN app and want to deploy it.It is working fine in my system but when I deploy it on Heroku I'm getting an application error.I run npm start to check for any error in Heroku console but console logged a "server started" success message which means node server is working fine.
我构建了我的第一个恶意应用,并想要部署它。它在我的系统中工作得很好,但是当我在Heroku上部署它时,我得到了一个应用程序错误。我运行npm start检查Heroku控制台中的任何错误,但是控制台记录了一个“服务器启动”成功消息,这意味着节点服务器运行良好。
app.js
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
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");
});
Here are heroku logs for my application :
这里是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 个解决方案
#1
2
Heroku documentation says you need to bind your port to PORT
. So you might want to change your code to
Heroku文档说需要将端口绑定到端口。因此,您可能想要将代码更改为
app.set('port', process.env.PORT || 4040);
#1
2
Heroku documentation says you need to bind your port to PORT
. So you might want to change your code to
Heroku文档说需要将端口绑定到端口。因此,您可能想要将代码更改为
app.set('port', process.env.PORT || 4040);