如何解决在Heroku上部署的MEAN Stack应用程序给出错误?

时间:2020-07-16 11:28:55

标签: node.js angular heroku npm heroku-cli

使用api node.js构建angular应用后,部署heroku会出现以下错误。但是当我在localserver上运行我的应用程序时,它运行正常,没有错误。

这是我的heroku 日志

C:\Users\PC>heroku logs -a=ts-ecommerce-project
2020-07-16T10:55:21.649783+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=ts-ecommerce-project.herokuapp.com request_id=568f7b4d-43b3-455b-803c-6e57b76e79be fwd="106.207.242.158" dyno= connect= service= status=503 bytes= protocol=https
2020-07-16T10:55:22.206857+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=ts-ecommerce-project.herokuapp.com request_id=bd96231e-5998-4eef-9b3b-9531bda070fc fwd="106.207.242.158" dyno= connect= service= status=503 bytes= protocol=https
2020-07-16T11:03:30.717571+00:00 app[api]: Rollback to v9 by user it171056@global.org.in
2020-07-16T11:03:30.717571+00:00 app[api]: Release v11 created by user it171056@global.org.in
2020-07-16T11:03:32.639092+00:00 heroku[web.1]: State changed from crashed to starting
2020-07-16T11:03:35.769681+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-16T11:03:38.672581+00:00 app[web.1]:
2020-07-16T11:03:38.672611+00:00 app[web.1]: > backend@0.0.0 start /app
2020-07-16T11:03:38.672612+00:00 app[web.1]: > node ./bin/www
2020-07-16T11:03:38.672612+00:00 app[web.1]:
2020-07-16T11:03:39.310220+00:00 app[web.1]: Port 80 requires elevated privileges
2020-07-16T11:03:39.318242+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-16T11:03:39.318623+00:00 app[web.1]: npm ERR! errno 1
2020-07-16T11:03:39.320174+00:00 app[web.1]: npm ERR! backend@0.0.0 start: `node ./bin/www`
2020-07-16T11:03:39.320401+00:00 app[web.1]: npm ERR! Exit status 1
2020-07-16T11:03:39.320636+00:00 app[web.1]: npm ERR!
2020-07-16T11:03:39.320820+00:00 app[web.1]: npm ERR! Failed at the backend@0.0.0 start script.
2020-07-16T11:03:39.320988+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-16T11:03:39.328462+00:00 app[web.1]:
2020-07-16T11:03:39.328701+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-16T11:03:39.328843+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-07-16T11_03_39_323Z-debug.log
2020-07-16T11:03:39.392239+00:00 heroku[web.1]: Process exited with status 1
2020-07-16T11:03:39.429206+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-16T11:03:39.431919+00:00 heroku[web.1]: State changed from crashed to starting
2020-07-16T11:03:42.993576+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-16T11:03:47.260974+00:00 app[web.1]:
2020-07-16T11:03:47.260994+00:00 app[web.1]: > backend@0.0.0 start /app
2020-07-16T11:03:47.260994+00:00 app[web.1]: > node ./bin/www
2020-07-16T11:03:47.260995+00:00 app[web.1]:
2020-07-16T11:03:48.222470+00:00 app[web.1]: Port 80 requires elevated privileges
2020-07-16T11:03:48.244977+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-16T11:03:48.245462+00:00 app[web.1]: npm ERR! errno 1
2020-07-16T11:03:48.246678+00:00 app[web.1]: npm ERR! backend@0.0.0 start: `node ./bin/www`
2020-07-16T11:03:48.246926+00:00 app[web.1]: npm ERR! Exit status 1
2020-07-16T11:03:48.247101+00:00 app[web.1]: npm ERR!
2020-07-16T11:03:48.247244+00:00 app[web.1]: npm ERR! Failed at the backend@0.0.0 start script.
2020-07-16T11:03:48.247392+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-16T11:03:48.446363+00:00 app[web.1]:
2020-07-16T11:03:48.446697+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-16T11:03:48.446859+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-07-16T11_03_48_248Z-debug.log
2020-07-16T11:03:48.508496+00:00 heroku[web.1]: Process exited with status 1
2020-07-16T11:03:48.562957+00:00 heroku[web.1]: State changed from starting to crashed

这是我的config.js文件 config.js

// check env.
var env = process.env.NODE_ENV || 'development';
// fetch env. config
var config = require('./config.json');
var envConfig = config[env];
// add env. config values to process.env
Object.keys(envConfig).forEach(key => process.env[key] = envConfig[key]);

这是我的config.json文件 config.json

{
    "development": {
        "PORT": 3000,
        "MONGODB_URI": "mongodb://localhost:27017/MEANStackDB",
        "JWT_SECRET": "SECRET#123",
        "JWT_EXP": "1h"
    },
    "production": {
        "PORT": 80,
        "MONGODB_URI": "mongodb://xxxxx/DB_Name",
        "JWT_SECRET": "SECRET#123",
        "JWT_EXP": "1h"
    }
}

当前,我将我的项目设置为在端口3000上运行的开发模式

2 个答案:

答案 0 :(得分:0)

将此添加到您的app.jsserver.js文件中。

Heroku设置您应该绑定的PORT变量。

const PORT = process.env.PORT || 80;

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));

答案 1 :(得分:0)

您不能在heroku上使用端口80。请像下面这样在代码中设置您正在侦听的端口:

port = process.env.PORT || 80

Heroku应该自动设置代理,以便端口80上的URL重定向到它在process.env.PORT中设置的端口。