2

I deployed my app to heroku fine with no problems. I was able to run heroku local web on localhost:5000 and it worked. When I went to the web dyno address, it said application error. I checked the logs for the site and it said:

 2017-05-01T02:52:30.980217+00:00 app[web.1]:     at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:274:12)
 2017-05-01T02:52:30.980218+00:00 app[web.1]:     at emitTwo (events.js:106:13)
 2017-05-01T02:52:30.980218+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
 2017-05-01T02:52:30.980218+00:00 app[web.1]:     at Connection.emit (events.js:191:7)
 2017-05-01T02:52:30.980219+00:00 app[web.1]:     at emitOne (events.js:96:13)
 2017-05-01T02:52:30.980220+00:00 app[web.1]:     at Socket.emit (events.js:188:7)
 2017-05-01T02:52:30.980221+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:104:9)
 2017-05-01T02:52:31.040828+00:00 heroku[web.1]: State changed from starting to crashed
 2017-05-01T02:52:31.029211+00:00 heroku[web.1]: Process exited with status 1
 2017-05-01T02:52:40.638415+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=room111-thoughts.herokuapp.com request_id=91b591cf-3d8d-4d94-8caf-7b1b868b088b fwd="108.221.62.78" dyno= connect= service= status=503 bytes= protocol=https
 2017-05-01T02:52:41.600924+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=room111-thoughts.herokuapp.com request_id=c8f6c2d3-dc38-4e65-9a20-581910f42b36 fwd="108.221.62.78" dyno= connect= service= status=503 bytes= protocol=https

I am using node.js and server.js (the initializing script) is as follows

// load environment variables
require('dotenv').config();

// grab our dependencies
const express    = require('express'),
  app            = express(),
  port           = process.env.PORT || 8080,
  expressLayouts = require('express-ejs-layouts'),
  mongoose       = require('mongoose'),
  bodyParser     = require('body-parser'),
  session        = require('express-session'),
  cookieParser   = require('cookie-parser'),
  flash          = require('connect-flash'),
  expressValidator = require('express-validator');

// configure our application ===================
// set sessions and cookie parser
app.use(cookieParser());
app.use(session({
  secret: process.env.SECRET, 
  cookie: { maxAge: 60000 },
  resave: false,    // forces the session to be saved back to the store
  saveUninitialized: false  // dont save unmodified
}));
app.use(flash());

// tell express where to look for static assets
app.use(express.static(__dirname + '/public'));

// set ejs as our templating engine
app.set('view engine', 'ejs');
app.use(expressLayouts);

// connect to our database
mongoose.connect(process.env.DB_URI);

// use body parser to grab info from a form
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());

// set the routes =============================
app.use(require('./app/routes'));

// start our server ===========================
app.listen(port, () => {
  console.log(`App listening on http://localhost:${port}`);
});

I don't have any idea what is happening, especially because it worked fine on localhost and deployment. Any help is appreciated.

3 Answers3

2

I had the exact same problem. I was not using mongo db but the error was the same:

2018-01-23T09:30:10.806568+00:00 heroku[web.1]: Process exited with status 1
2018-01-23T09:30:10.823360+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-23T09:30:32.001596+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"....

The app was perfectly working on localhost but crashed when deployed on heroku. Heroku website logs page was not helping at all so I tried locally by command line: heroku logs --tail and many logs not showed in the website came out. Those logs said :

2018-01-23T09:30:09.431324+00:00 heroku[web.1]: Starting process with command `node server.js`
2018-01-23T09:30:10.771449+00:00 app[web.1]: module.js:540
2018-01-23T09:30:10.771465+00:00 app[web.1]:     throw err;
2018-01-23T09:30:10.771466+00:00 app[web.1]:     ^
2018-01-23T09:30:10.771468+00:00 app[web.1]:
2018-01-23T09:30:10.771470+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:538:15)
2018-01-23T09:30:10.771469+00:00 app[web.1]: Error: Cannot find module 'express'

I realized I had to install express, so i run:

npm install express
git push heroku master
heroku open 

and it worked. Hopefully it will work for you as well!!

IronJohn
  • 306
  • 2
  • 8
0

Check the environment variable DB_URI by calling heroku config and seeing what is listed for it. The error indicates that your connection is being refused, so my guess is that there is no mongo db configured or that the environment variable isn't set in the heroku app context.

Paul
  • 35,689
  • 11
  • 93
  • 122
0

For me, the answer above about the environment variable was it. Mine was related to the variable MONGODB_URI not being set as an environment variable in heroku, and running heroku config returned with no variables in the cli.

The fix was running : heroku addons:create mongolab:sandbox

If heroku push is needed, then heroku config should now show your .env variable.