Mern应用在本地运行-Heroku(H10-H13)上出现503错误

时间:2020-05-17 10:02:26

标签: node.js reactjs mongodb heroku mern

我通过与Brad Traversy一起称为联络人的课程完成了一个项目。它在我的机器上本地正常运行,并且我与数据库联系。

一旦将其推送到Heroku,我将无法登录或注册。我收到503错误(来自heroku的H10和H13错误),说在尝试访问这些端点时服务不可用。

我尝试寻找类似的线程,但对我而言不起作用。我已经添加了一个procfile并检查了package.json,从我看来,它看起来还不错。

完整的存储库可以在这里找到:https://github.com/Dannus90/Contact_Keeper_MERN

const express = require('express');
const connectDB = require('./config/db');
const path = require('path');

const app = express();

// Connect Database
connectDB();

// Init Middleware
app.use(express.json({ extended: false }));

// Define Routes
app.use('/api/users', require('./routes/users'));
app.use('/api/auth', require('./routes/auth'));
app.use('/api/contacts', require('./routes/contacts'));

// Serve static assets in production
if (process.env.NODE_ENV === 'production') {
  // Set static folder
  app.use(express.static('client/build'));

  app.get('*', (req, res) =>
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
  );
}

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

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

在下面的图片中,您可以查看失败的地方。当它尝试命中端点时,它位于AuthState文件中。

Where it fails img

来自heroku的完整错误日志可以在下面看到:

2020-05-17T09:23:20.616623+00:00 heroku[web.1]: State changed from starting to up
2020-05-17T09:23:50.230066+00:00 app[web.1]: connection <monitor> to 52.31.65.44:27017 closed
2020-05-17T09:23:50.240114+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-05-17T09:23:50.240313+00:00 app[web.1]: npm ERR! errno 1
2020-05-17T09:23:50.241253+00:00 app[web.1]: npm ERR! contact-keeper@1.0.0 start: `node server.js`
2020-05-17T09:23:50.241374+00:00 app[web.1]: npm ERR! Exit status 1
2020-05-17T09:23:50.241498+00:00 app[web.1]: npm ERR!
2020-05-17T09:23:50.241622+00:00 app[web.1]: npm ERR! Failed at the contact-keeper@1.0.0 start script.
2020-05-17T09:23:50.241760+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.  
2020-05-17T09:23:50.249534+00:00 app[web.1]: 
2020-05-17T09:23:50.249723+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-05-17T09:23:50.249843+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-05-17T09_23_50_242Z-debug.log
2020-05-17T09:23:50.311445+00:00 heroku[web.1]: State changed from up to crashed
2020-05-17T09:26:49.000000+00:00 app[api]: Build started by user persson.daniel.1990@gmail.com
2020-05-17T09:28:20.734327+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-17T09:28:20.461350+00:00 app[api]: Release v48 created by user persson.daniel.1990@gmail.com
2020-05-17T09:28:20.461350+00:00 app[api]: Deploy 502ce232 by user persson.daniel.1990@gmail.com
2020-05-17T09:28:21.000000+00:00 app[api]: Build succeeded
2020-05-17T09:28:30.543747+00:00 app[web.1]:
2020-05-17T09:28:30.543761+00:00 app[web.1]: > contact-keeper@1.0.0 start /app
2020-05-17T09:28:30.543761+00:00 app[web.1]: > node server.js
2020-05-17T09:28:30.543762+00:00 app[web.1]:
2020-05-17T09:28:31.157953+00:00 app[web.1]: Server started on port 13966
2020-05-17T09:28:32.044290+00:00 heroku[web.1]: State changed from starting to up
2020-05-17T09:28:33.217189+00:00 heroku[router]: at=info method=GET path="/" host=afternoon-earth-34040.herokuapp.com request_id=ed1492f5-d053-4ecf-8a88-8561992f3403 fwd="78.82.2.105" dyno=web.1 connect=0ms service=11ms status=200 bytes=2626 protocol=https
2020-05-17T09:28:33.348570+00:00 heroku[router]: at=info method=GET path="/static/css/main.abec718a.chunk.css" host=afternoon-earth-34040.herokuapp.com request_id=410da44d-797f-4a41-bafe-f130118e8c6d fwd="78.82.2.105" dyno=web.1 connect=0ms service=4ms status=200 bytes=4578 protocol=https 
2020-05-17T09:28:33.346039+00:00 heroku[router]: at=info method=GET path="/static/js/main.6e77bd50.chunk.js" host=afternoon-earth-34040.herokuapp.com request_id=018e556e-91d6-4944-bd6e-0c5b74ece68a fwd="78.82.2.105" dyno=web.1 connect=0ms service=4ms status=200 bytes=28978 protocol=https  
2020-05-17T09:28:33.474881+00:00 heroku[router]: at=info method=GET path="/static/js/2.9c49eab7.chunk.js" host=afternoon-earth-34040.herokuapp.com request_id=5379490e-fd9b-47c1-b001-15e036784e2d fwd="78.82.2.105" dyno=web.1 connect=0ms service=10ms status=200 bytes=192222 protocol=https   
2020-05-17T09:28:33.992494+00:00 heroku[router]: at=info method=GET path="/api/contacts" host=afternoon-earth-34040.herokuapp.com request_id=3684bbba-83b1-4d90-b9f8-d4ba7dedbc60 fwd="78.82.2.105" dyno=web.1 connect=0ms service=2ms status=401 bytes=257 protocol=https
2020-05-17T09:28:33.990977+00:00 heroku[router]: at=info method=GET path="/api/auth" host=afternoon-earth-34040.herokuapp.com request_id=705c0086-3927-4e6f-b2dd-45ac9165e303 fwd="78.82.2.105" dyno=web.1 connect=0ms service=4ms status=401 bytes=257 protocol=https
2020-05-17T09:28:34.365542+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=afternoon-earth-34040.herokuapp.com request_id=1f38b786-2c3d-411e-8aee-6862c40e77d4 fwd="78.82.2.105" dyno=web.1 connect=1ms service=6ms status=200 bytes=787 protocol=https
2020-05-17T09:28:34.483877+00:00 heroku[router]: at=info method=GET path="/logo192.png" host=afternoon-earth-34040.herokuapp.com request_id=65807958-8954-4332-8cab-dc4320e2cac8 fwd="78.82.2.105" dyno=web.1 connect=0ms service=2ms status=200 bytes=5622 protocol=https
2020-05-17T09:28:42.084546+00:00 heroku[router]: at=info method=GET path="/static/js/2.9c49eab7.chunk.js.map" host=afternoon-earth-34040.herokuapp.com request_id=d186f408-b227-4ce3-8f6d-2858c2b6b5a0 fwd="78.82.2.105" dyno=web.1 connect=0ms service=18ms status=200 bytes=624338 protocol=https
2020-05-17T09:28:42.056584+00:00 heroku[router]: at=info method=GET path="/static/css/main.abec718a.chunk.css" host=afternoon-earth-34040.herokuapp.com request_id=4ebde32e-fca4-4968-89d2-e10af56e5ad3 fwd="78.82.2.105" dyno=web.1 connect=0ms service=4ms status=304 bytes=238 protocol=https  
2020-05-17T09:28:42.176331+00:00 heroku[router]: at=info method=GET path="/static/js/main.6e77bd50.chunk.js.map" host=afternoon-earth-34040.herokuapp.com request_id=22d537d1-9c6e-445b-985a-78f2319dd192 fwd="78.82.2.105" dyno=web.1 connect=0ms service=4ms status=200 bytes=61221 protocol=https
2020-05-17T09:28:42.289347+00:00 heroku[router]: at=info method=GET path="/static/css/main.abec718a.chunk.css.map" host=afternoon-earth-34040.herokuapp.com request_id=7418f08d-9c43-47e0-b2d3-2f2d23652356 fwd="78.82.2.105" dyno=web.1 connect=0ms service=2ms status=200 bytes=15892 protocol=https
2020-05-17T09:29:01.254923+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/auth" host=afternoon-earth-34040.herokuapp.com request_id=82f5a82c-e065-4e9e-a284-a82ba86287d2 fwd="78.82.2.105" dyno=web.1 connect=0ms service=15877ms status=503 bytes=0 protocol=https
2020-05-17T09:29:01.362981+00:00 heroku[web.1]: State changed from up to crashed
2020-05-17T09:29:01.235846+00:00 app[web.1]: connection <monitor> to 54.72.107.147:27017 closed
2020-05-17T09:29:01.262738+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-05-17T09:29:01.263049+00:00 app[web.1]: npm ERR! errno 1
2020-05-17T09:29:01.263998+00:00 app[web.1]: npm ERR! contact-keeper@1.0.0 start: `node server.js`
2020-05-17T09:29:01.264159+00:00 app[web.1]: npm ERR! Exit status 1
2020-05-17T09:29:01.264337+00:00 app[web.1]: npm ERR!
2020-05-17T09:29:01.264484+00:00 app[web.1]: npm ERR! Failed at the contact-keeper@1.0.0 start script.
2020-05-17T09:29:01.264618+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.  
2020-05-17T09:29:01.270789+00:00 app[web.1]:
2020-05-17T09:29:01.270997+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-05-17T09:29:01.271145+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-05-17T09_29_01_265Z-debug.log
2020-05-17T09:29:01.256625+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/auth" host=afternoon-earth-34040.herokuapp.com request_id=0e0e84a4-257b-43bd-a31d-b97ebaed5a10 fwd="78.82.2.105" dyno=web.1 connect=0ms service=16031ms status=503 bytes=0 protocol=https
2020-05-17T09:29:01.256627+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/auth" host=afternoon-earth-34040.herokuapp.com request_id=acbb4fdf-bf07-4668-886e-a253a3e0aee3 fwd="78.82.2.105" dyno=web.1 connect=0ms service=15442ms status=503 bytes=0 protocol=https
2020-05-17T09:29:01.257557+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/auth" host=afternoon-earth-34040.herokuapp.com request_id=1a45a04e-0c1e-47ea-b796-7299db72f732 fwd="78.82.2.105" dyno=web.1 connect=0ms service=21480ms status=503 bytes=0 protocol=https
2020-05-17T09:29:01.257996+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/auth" host=afternoon-earth-34040.herokuapp.com request_id=2531aa80-5bf8-42e8-b620-ff9f2969ba1f fwd="78.82.2.105" dyno=web.1 connect=0ms service=15579ms status=503 bytes=0 protocol=https
2020-05-17T09:29:04.000000+00:00 app[api]: Build started by user persson.daniel.1990@gmail.com
2020-05-17T09:30:32.857023+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-17T09:30:32.560586+00:00 app[api]: Release v49 created by user persson.daniel.1990@gmail.com
2020-05-17T09:30:32.560586+00:00 app[api]: Deploy aba0d37e by user persson.daniel.1990@gmail.com
2020-05-17T09:30:34.000000+00:00 app[api]: Build succeeded
2020-05-17T09:30:44.786119+00:00 app[web.1]:
2020-05-17T09:30:44.786136+00:00 app[web.1]: > contact-keeper@1.0.0 start /app
2020-05-17T09:30:44.786137+00:00 app[web.1]: > node server.js
2020-05-17T09:30:44.786137+00:00 app[web.1]:
2020-05-17T09:30:45.574040+00:00 app[web.1]: Server started on port 52681
2020-05-17T09:30:46.081535+00:00 heroku[web.1]: State changed from starting to up
2020-05-17T09:31:15.677435+00:00 app[web.1]: connection <monitor> to 52.31.65.44:27017 closed
2020-05-17T09:31:15.698356+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-05-17T09:31:15.698666+00:00 app[web.1]: npm ERR! errno 1
2020-05-17T09:31:15.699733+00:00 app[web.1]: npm ERR! contact-keeper@1.0.0 start: `node server.js`
2020-05-17T09:31:15.699922+00:00 app[web.1]: npm ERR! Exit status 1
2020-05-17T09:31:15.700135+00:00 app[web.1]: npm ERR!
2020-05-17T09:31:15.700312+00:00 app[web.1]: npm ERR! Failed at the contact-keeper@1.0.0 start script.
2020-05-17T09:31:15.700467+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.  
2020-05-17T09:31:15.723946+00:00 app[web.1]:
2020-05-17T09:31:15.724237+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-05-17T09:31:15.724392+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-05-17T09_31_15_704Z-debug.log
2020-05-17T09:31:15.828237+00:00 heroku[web.1]: State changed from up to crashed
2020-05-17T09:35:10.524783+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-17T09:35:21.345206+00:00 app[web.1]:
2020-05-17T09:35:21.345238+00:00 app[web.1]: > contact-keeper@1.0.0 start /app
2020-05-17T09:35:21.345238+00:00 app[web.1]: > node server.js
2020-05-17T09:35:21.345239+00:00 app[web.1]:
2020-05-17T09:35:22.181653+00:00 app[web.1]: Server started on port 57715
2020-05-17T09:35:22.526258+00:00 heroku[web.1]: State changed from starting to up
2020-05-17T09:35:52.241989+00:00 app[web.1]: connection <monitor> to 54.72.107.147:27017 closed
2020-05-17T09:35:52.255501+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-05-17T09:35:52.256048+00:00 app[web.1]: npm ERR! errno 1
2020-05-17T09:35:52.257707+00:00 app[web.1]: npm ERR! contact-keeper@1.0.0 start: `node server.js`
2020-05-17T09:35:52.258009+00:00 app[web.1]: npm ERR! Exit status 1
2020-05-17T09:35:52.258313+00:00 app[web.1]: npm ERR!
2020-05-17T09:35:52.258505+00:00 app[web.1]: npm ERR! Failed at the contact-keeper@1.0.0 start script.
2020-05-17T09:35:52.258686+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.  
2020-05-17T09:35:52.266384+00:00 app[web.1]:
2020-05-17T09:35:52.266689+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-05-17T09:35:52.266864+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-05-17T09_35_52_259Z-debug.log
2020-05-17T09:35:52.425747+00:00 heroku[web.1]: State changed from up to crashed
2020-05-17T09:37:40.075229+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=afternoon-earth-34040.herokuapp.com request_id=b86ff65a-3c2f-4609-85ba-a75500f93e3a fwd="78.82.2.105" dyno= connect= service= status=503 bytes= protocol=https
2020-05-17T09:37:40.189972+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=afternoon-earth-34040.herokuapp.com request_id=d5b50b56-a5df-4d19-9c0f-6298c0dea421 fwd="78.82.2.105" dyno= connect= service= status=503 bytes= protocol=https
2020-05-17T09:37:42.254860+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/login" host=afternoon-earth-34040.herokuapp.com request_id=20cdf3b9-5883-4dcd-898a-03eb6ee20dc5 fwd="78.82.2.105" dyno= connect= service= status=503 bytes= protocol=https
2020-05-17T09:37:43.970046+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/login" host=afternoon-earth-34040.herokuapp.com request_id=dc0e7da8-46fa-4da1-bdb6-4814157e43a5 fwd="78.82.2.105" dyno= connect= service= status=503 bytes= protocol=https
2020-05-17T09:37:44.242232+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=afternoon-earth-34040.herokuapp.com request_id=53adb339-25bb-4bca-9b89-638035820973 fwd="78.82.2.105" dyno= connect= service= status=503 bytes= protocol=https

这是我的数据库文件中的代码:

const mongoose = require("mongoose");
const config = require("config");
const db = config.get("mongoURI");

const connectDB = async () => {
    try {
        await mongoose.connect(db, {
            useNewUrlParser: true,
            useCreateIndex: true,
            useFindAndModify: false,
            useUnifiedTopology: true,
        });
        console.log("MongoDB Connected");
    } catch (err) {
        console.error(err.message);
        process.exit(1);
    }
};

module.exports = connectDB;

这是数据库(位于default和production.js中):

{
    "mongoURI": "mongodb+srv://Daniel123:Daniel123@contactkeeper-rjjr4.mongodb.net/test?retryWrites=true&w=majority",
    "jwtSecret": "secret"
} 

1 个答案:

答案 0 :(得分:0)

为我解决此问题(如果其他人遇到问题):

我将mongodb上的0.0.0.0/0列入了白名单,在heroku上设置了环境变量,并将它们设置为db connect的替代项(与config中的jwtSecret相同),因为在推送到heroku时会忽略该配置:

const mongoose = require("mongoose");
const config = require("config");
const db = config.get("mongoURI");

const connectDB = async () => {
    try {
        await mongoose.connect(db || process.env.mongoURI, {
            useNewUrlParser: true,
            useCreateIndex: true,
            useFindAndModify: false,
            useUnifiedTopology: true,
        });
        console.log("MongoDB Connected");
    } catch (err) {
        console.error(err.message);
        process.exit(1);
    }
};

module.exports = connectDB;