将MERN应用程序部署到Heroku时出错(前端已成功部署,但后端未成功部署)

时间:2020-02-05 02:52:40

标签: node.js mongodb express heroku mern

我在本节中苦苦挣扎了大约10多个小时。我尝试对此进行研究并发现问题所在。我尝试了一些解决方案,但似乎无法为我工作。我已经按照BradTraversy的方式进行了所有设置,但是由于某种原因,我的部署没有完成。本地开发在前端和后端都可以正常工作。

问题是当我将产品部署到Heroku上时。它显示了前端,但是后端服务器决定不工作。我将在此行之后将存储库发布到仓库。

非常感谢你们抽出宝贵的时间来帮助我。

存储库:https://github.com/bradtraversy/devconnector_2.0

我通过输入以下内容将应用程序部署到Heroku:

git push heroku master

然后我通过输入以下内容来检查日志:

heroku logs --tail

我在此日志中遇到的错误是:

heroku [路由器]:at =错误代码= H13 desc =“连接已关闭,无响应” method = POST path =“ / api / users service = 10164ms status = 503”

heroku [路由器]:at =错误代码= H10 desc =“应用程序崩溃”方法= POST路径=“ / api / users” service = status = 503

2020-02-05T02:14:30.248090+00:00 heroku[web.1]: State changed from up to crashed
2020-02-05T02:14:30.225433+00:00 heroku[web.1]: Process exited with status 1
2020-02-05T02:24:44.491342+00:00 heroku[web.1]: State changed from crashed to starting
2020-02-05T02:24:52.827814+00:00 heroku[web.1]: Starting process with command `npm start`
2020-02-05T02:24:55.035533+00:00 app[web.1]: 
2020-02-05T02:24:55.035564+00:00 app[web.1]: > devconnector@1.0.0 start /app
2020-02-05T02:24:55.035566+00:00 app[web.1]: > node server.js
2020-02-05T02:24:55.035568+00:00 app[web.1]: 
2020-02-05T02:24:55.719810+00:00 app[web.1]: Server started on port 58394
2020-02-05T02:24:56.130106+00:00 heroku[web.1]: State changed from starting to up
2020-02-05T02:25:25.780242+00:00 app[web.1]: Server selection timed out after 30000 ms
2020-02-05T02:25:25.798194+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-02-05T02:25:25.798598+00:00 app[web.1]: npm ERR! errno 1
2020-02-05T02:25:25.799936+00:00 app[web.1]: npm ERR! devconnector@1.0.0 start: `node server.js`
2020-02-05T02:25:25.800096+00:00 app[web.1]: npm ERR! Exit status 1
2020-02-05T02:25:25.800260+00:00 app[web.1]: npm ERR!
2020-02-05T02:25:25.800376+00:00 app[web.1]: npm ERR! Failed at the devconnector@1.0.0 start script.
2020-02-05T02:25:25.800502+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-02-05T02:25:25.809573+00:00 app[web.1]: 
2020-02-05T02:25:25.809899+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-02-05T02:25:25.810391+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-02-05T02_25_25_801Z-debug.log
2020-02-05T02:25:25.929540+00:00 heroku[web.1]: State changed from up to crashed
2020-02-05T02:25:25.910090+00:00 heroku[web.1]: Process exited with status 1
2020-02-05T02:26:34.034101+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=c4fd2b74-4a3b-4857-b954-81ee18c94b8a fwd="71.178.243.54" dyno= connect= service= status=503 bytes= protocol=https

2020-02-05T02:30:24.143711+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=306e3957-63bc-4927-a8d7-d92cfd73790d fwd="71.178.243.54" dyno=web.1 connect=0ms service=8196ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.137543+00:00 app[web.1]: Server selection timed out after 30000 ms
2020-02-05T02:30:24.146314+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-02-05T02:30:24.146527+00:00 app[web.1]: npm ERR! errno 1
2020-02-05T02:30:24.147415+00:00 app[web.1]: npm ERR! devconnector@1.0.0 start: `node server.js`
2020-02-05T02:30:24.147505+00:00 app[web.1]: npm ERR! Exit status 1
2020-02-05T02:30:24.147637+00:00 app[web.1]: npm ERR!
2020-02-05T02:30:24.147718+00:00 app[web.1]: npm ERR! Failed at the devconnector@1.0.0 start script.
2020-02-05T02:30:24.147802+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-02-05T02:30:24.153368+00:00 app[web.1]: 
2020-02-05T02:30:24.153531+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-02-05T02:30:24.153633+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-02-05T02_30_24_148Z-debug.log
2020-02-05T02:30:24.230309+00:00 heroku[web.1]: State changed from up to crashed
2020-02-05T02:30:24.141581+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=810181a6-17d8-4718-a417-18e54decccdd fwd="71.178.243.54" dyno=web.1 connect=0ms service=10164ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.143736+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=ea00ea5c-3403-4db9-8df6-d895c612dc3b fwd="71.178.243.54" dyno=web.1 connect=0ms service=8826ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.142003+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users" host=floating-eyrie-69630.herokuapp.com request_id=62b8da0b-d894-4d38-9c2d-496833c0ec14 fwd="71.178.243.54" dyno=web.1 connect=0ms service=9308ms status=503 bytes=0 protocol=https
2020-02-05T02:30:24.212393+00:00 heroku[web.1]: Process exited with status 1

仅需注意几件事,我确实已正确设置了所有内容。我设置了正确输入用户名和密码的mongoURI

config / production.json

{
    "mongoURI": "mongodb+srv://myusername:mypassword@cluster0-ziuao.mongodb.net/test?retryWrites=true&w=majority",
    "jwtSecret": "secret",
    "githubClientId": "",
    "githubSecret": ""
}

配置/数据库

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);
        // Exit process with failure
        process.exit(1);
    }
};

module.exports = connectDB;

server.js

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/api/users'));
app.use('/api/auth', require('./routes/api/auth'));
app.use('/api/profile', require('./routes/api/profile'));
app.use('/api/posts', require('./routes/api/posts'));

// 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}`));

我想知道是什么原因造成的?任何有用的答案和/或向正确方向的推动将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:1)

解决方案

我向MongoDB集群添加了网络访问权限,以便可以从任何地方访问。

我对自己很生气,哈哈。躺在床上时,我对此有何想法呢? :')

答案 1 :(得分:0)

我个人在使用Heroku时遇到了问题,因为我使用的是"./public"之类的文件路径,并将其编写为__dirname + "/public"可以为我解决。 Heroku可能会弄乱您的文件结构?