部署到Heroku的Express / sequelize app提供SequelizeConnectionRefusedError

时间:2016-12-03 12:24:55

标签: express heroku

我有一个快速/续集应用程序,可以在我的机器上正常开发。我试图通过Heroku部署它。我有一个链接到我的heroku应用程序的postgres数据库。构建通过,但应用程序不会启动。 Heroku日志的错误消息是:

  

2016-12-03T11:26:06.614800 + 00:00 app [web.1]:未处理拒绝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   连接。 (/app/node_modules/pg/lib/client.js:185:5)   2016-12-03T11:26:06.614810 + 00:00 app [web.1]:在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]:在Socket。   (/app/node_modules/pg/lib/connection.js:71:10)   2016-12-03T11:26:06.614812 + 00:00 app [web.1]:在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]:在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]清除退出 -   重启前等待更改2016-12-03T11:27:01.296756 + 00:00   heroku [web.1]:错误R10(启动超时) - > Web进程无法绑定   发布后60秒内到$ PORT 2016-12-03T11:27:01.296831 + 00:00   heroku [web.1]:使用SIGKILL停止进程   2016-12-03T11:27:01.454936 + 00:00 heroku [web.1]:州改变自   开始崩溃

我认为这是生产环境的数据库设置问题。我花了很长时间用谷歌搜索,现在真的需要一些帮助来推动我朝着正确的方向前进!提前谢谢。

应用/ 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 + '.');
});

应用/配置/ 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"
  }
}

应用/模型/ 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;

2 个答案:

答案 0 :(得分:7)

我想回答我自己的问题,以防其他人遇到同样的问题。我更新了config.json文件的生产元素,如下所示:

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

现在它完美无缺!

答案 1 :(得分:0)

我遇到了同样的错误,结果我忘记在附加组件上添加 JawsDB MySQL。 并且不要忘记在您的连接中使用 process.env.Jawsdb_url