Postgres和Sequelize的Heroku部署问题

时间:2017-05-02 17:52:09

标签: postgresql heroku deployment environment-variables sequelize.js

我正在尝试将我的应用部署到Heroku。它在本地运行时工作得很好。从我部署它的那一刻起(通过github集成部署),我收到了通用的Application Error屏幕。以下是我的heroku日志。这些实际上运行了三次,但在我发布三次相同的事情没有任何意义。他们总是以:

结束
2017-05-02T14:59:26.033702+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=calm-crag-40902.herokuapp.com request_id=6b64883e-9697-4d58-84d9-2f173d5b4cb1 fwd="70.
54.76.222" dyno= connect= service= status=503 bytes= protocol=https
2017-05-02T14:59:27.425435+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=calm-crag-40902.herokuapp.com request_id=d3719d8f-4670-4621-a044-a511ee6884
10 fwd="70.54.76.222" dyno= connect= service= status=503 bytes= protocol=https

每次连续错误屏幕后,这将打印2至4次。第一个错误是关于.env文件没有运行,第二个错误是尝试连接到sequelize。我主要关注的是整理.env,因为如果整理出来,至少应用程序会显示我的应用程序将会显示。

.env错误:

{ Error: ENOENT: no such file or directory, open '.env'
2017-05-02T16:09:39.622865+00:00 app[web.1]:     at Error (native)
2017-05-02T16:09:39.622866+00:00 app[web.1]:     at Object.fs.openSync (fs.js:641:18)
2017-05-02T16:09:39.622867+00:00 app[web.1]:     at Object.fs.readFileSync (fs.js:509:33)
2017-05-02T16:09:39.622867+00:00 app[web.1]:     at Object.config (/app/node_modules/dotenv/lib/main.js:30:37)
2017-05-02T16:09:39.622868+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:3:19)
2017-05-02T16:09:39.622869+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-05-02T16:09:39.622869+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-05-02T16:09:39.622870+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-02T16:09:39.622871+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-02T16:09:39.622871+00:00 app[web.1]:     at Function.Module._load (module.js:438:3) errno: -2, code: 'ENOENT', syscall: 'open', path: '.env' }
2017-05-02T16:09:40.242885+00:00 app[web.1]: Unhandled rejection

Sequelize错误:

SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432
2017-05-02T16:09:40.242902+00:00 app[web.1]:     at /app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20
2017-05-02T16:09:40.242903+00:00 app[web.1]:     at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:186:5)
2017-05-02T16:09:40.242904+00:00 app[web.1]:     at emitOne (events.js:96:13)
2017-05-02T16:09:40.242905+00:00 app[web.1]:     at Connection.emit (events.js:188:7)
2017-05-02T16:09:40.242905+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:86:10)
2017-05-02T16:09:40.242906+00:00 app[web.1]:     at emitOne (events.js:96:13)
2017-05-02T16:09:40.242907+00:00 app[web.1]:     at emitErrorNT (net.js:1281:8)
2017-05-02T16:09:40.242907+00:00 app[web.1]:     at Socket.emit (events.js:188:7)
2017-05-02T16:09:40.242909+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:104:9)
2017-05-02T16:09:40.242908+00:00 app[web.1]:     at _combinedTickCallback (internal/process/next_tick.js:80:11)
2017-05-02T16:09:40.360035+00:00 heroku[web.1]: Process exited with status 0
2017-05-02T16:09:40.373996+00:00 heroku[web.1]: State changed from starting to crashed

这是我的index.js:

'use strict';

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

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

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

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

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

module.exports = db;

config.json:

{
  "development": {
    "username": "",
    "password": "",
    "database": "late_file",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  "test": {
    "username": "",
    "password": "",
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  "production": {
    "use_env_variable": "DATABASE_URL",
    "dialect": "postgres"
  }
}

.env,修改为排除事物的特定名称

DB_HOST=localhost
DB_USER=
DB_PASS=
DB_NAME=
DB_SSL=true if heroku
DB_PORT=5432
DATABASE_URL=postgres://appropriate/url

server.js的适用部分

const pg = require('pg');

pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
  if (err) throw err;
  console.log('Connected to postgres! Getting schemas...');

  client
    .query('SELECT table_schema,table_name FROM information_schema.tables;')
    .on('row', function(row) {
      console.log(JSON.stringify(row));
    });
});

任何帮助将不胜感激! 提前感谢任何提供支持的人!

1 个答案:

答案 0 :(得分:0)

布兰登,

我正在使用JAWS_DB和MySQL在Heroku上运行一个应用程序,但其他方面相同的是config.json和index.js。在Heroku的情况下,&#34; if(config.use_env_variable){const sequelize = new Sequelize(process.env [config.use_env_variable])&#34;语句连接到数据库而没有错误。日志似乎指向&#34; pg.connect&#34;在server.js中失败。我不清楚(没有看到你的其余代码)为什么你需要连接到index.js中的数据库,然后再次连接到server.js。如果您可以从pg.connect中删除连接并尝试仅使用index.js中的连接运行,则可能会停止此错误。