gpt4 book ai didi

angularjs - 在 Heroku 上部署 MEAN 应用程序时出现应用程序错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:15 25 4
gpt4 key购买 nike

我构建了我的第一个 MEAN 应用程序并想要部署它。它在我的系统中工作正常,但是当我将它部署在 Heroku 上时,我收到应用程序错误。我运行 npm start 检查 Heroku 控制台中是否有任何错误,但是控制台记录了一条“服务器已启动”成功消息,这意味着 Node 服务器工作正常。

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

最佳答案

Heroku documentation说你需要将你的端口绑定(bind)到PORT。所以你可能想将代码更改为

app.set('port', process.env.PORT || 4040);

关于angularjs - 在 Heroku 上部署 MEAN 应用程序时出现应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com