简体   繁体   中英

Error deploying MERN application to Heroku (Front-end deployed successfully, but the back-end didn't)

I've been struggling with this section for about 10+ hours. I tried doing my research on it and discovering the problem. I tried a few solutions but can't seem to get it to work for me. I have everything set up exactly the way BradTraversy has it, but for some reason, mine isn't deploying. Local development works perfectly fine with both the front and the back end of things.

The problem is that when I deploy to production onto Heroku. It shows the front-end, but then the back-end server decides not to work. I will post the repository to the repo following this line.

Thank you guys a ton for taking the time to help me out.

Repository: https://github.com/bradtraversy/devconnector_2.0

I deploy the app to Heroku by entering:

git push heroku master

Then I check the logs by entering:

heroku logs --tail

The errors that I've been getting in this log are:

heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/users service=10164ms status=503"

and

heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/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

Just a few things to note, I do have everything set up correctly. I have the mongoURI set with the username and password correctly entered

config/production.json

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

config/db

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

I'm wondering what could be causing this? Any helpful answers and/or a push to the correct direction will greatly be appreciated! Thanks!

SOLUTION

I added network access to my MongoDB cluster to be accessed from anywhere.

I'm so mad at myself, lol. Funny how I thought about this while I was laying in bed. :')

Personally I had issues with Heroku because I was using filepaths like "./public" and writing it __dirname + "/public" fixed it for me. Heroku maybe messes with your file structure?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM