Malinna Leach
Malinna Leach

Reputation: 71

Express/sequelize app deployed to Heroku giving SequelizeConnectionRefusedError

I have an express/sequelize app that works fine in development on my machine. I've tried to deploy it via Heroku. I have a postgres database linked to my heroku app. The build passes, but the app won't launch. The error message from Heroku logs is:

2016-12-03T11:26:06.614800+00:00 app[web.1]: Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432 2016-12-03T11:26:06.614808+00:00 app[web.1]: at /app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20 2016-12-03T11:26:06.614809+00:00 app[web.1]: at Connection. (/app/node_modules/pg/lib/client.js:185:5) 2016-12-03T11:26:06.614810+00:00 app[web.1]: at emitOne (events.js:96:13) 2016-12-03T11:26:06.614811+00:00 app[web.1]: at Connection.emit (events.js:188:7) 2016-12-03T11:26:06.614812+00:00 app[web.1]: at Socket. (/app/node_modules/pg/lib/connection.js:71:10) 2016-12-03T11:26:06.614812+00:00 app[web.1]: at emitOne (events.js:96:13) 2016-12-03T11:26:06.614813+00:00 app[web.1]: at Socket.emit (events.js:188:7) 2016-12-03T11:26:06.614814+00:00 app[web.1]: at emitErrorNT (net.js:1276:8) 2016-12-03T11:26:06.614814+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:74:11) 2016-12-03T11:26:06.614815+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:98:9) 2016-12-03T11:26:06.630985+00:00 app[web.1]: [nodemon] clean exit - waiting for changes before restart 2016-12-03T11:27:01.296756+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 2016-12-03T11:27:01.296831+00:00 heroku[web.1]: Stopping process with SIGKILL 2016-12-03T11:27:01.454936+00:00 heroku[web.1]: State changed from starting to crashed

I believe this is an issue with the database setup for the production environment. I've spent a long time googling and now really need some help to nudge me in the right direction! Thank you in advance.

app/server.js

const express = require('express'),
  bodyParser = require('body-parser'),
  cors = require('cors'),
  app = express();
const router = require('./router');
var models = require('./models');

var port = process.env.PORT || 3000;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
router(app);
app.set('view engine', 'ejs');

app.use(express.static(__dirname + '/public'));


models.sequelize.sync().then(function() {
  app.listen(port);
  console.log('Your server is running on port ' + port + '.');
});

app/config/config.json

{
  "development": {
    "username": null,
    "password": null,
    "database": "task_development",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  "test": {
    "username": null,
    "password": null,
    "database": "task_test",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  "production": {
    "username": null,
    "password": null,
    "database": "DATABASE_URL",
    "dialect": "postgres"
  }
}

app/models/index.js

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(module.filename);
var env       = process.env.NODE_ENV || 'development';
var config    = require(__dirname + '/../config/config.json')[env];
var db        = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(function(file) {
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Upvotes: 0

Views: 4085

Answers (2)

Fasika Walle
Fasika Walle

Reputation: 157

I got the same error and turns out I forgot to add JawsDB MySQL on the add ons. and don't forgot to use process.env.Jawsdb_url in you connection as well

Upvotes: 1

Malinna Leach
Malinna Leach

Reputation: 71

I wanted to answer my own question in case anyone else has the same problem. I updated the production element of my config.json file as below:

"production": {
    "use_env_variable": "DATABASE_URL"
  }

Now it works perfectly!

Upvotes: 7

Related Questions