0

我想部署我的應用程序到Heroku。在本地運行時,它工作得很好。從我部署它的那一刻起(通過github集成進行部署),我收到了通用應用程序錯誤屏幕。以下是我的heroku日誌。這些實際上運行了三次,但我沒有三次發佈同樣的東西。它們始終以:Heroku部署問題與Postgres和Sequelize

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

任何幫助的

適用部分將不勝感激! 預先感謝任何人提供任何支持!

回答

0

布蘭登,

我正在使用JAWS_DB和MySQL在Heroku上的應用程序,但完全一樣config.json和index.js。在我的Heroku的情況下,「if(config.use_env_variable){const sequelize = new Sequelize(process.env [config.use_env_variable])」語句無誤地連接到數據庫。日誌似乎指向server.js中的「pg.connect」失敗。我不清楚爲什麼你需要連接到index.js中的數據庫,然後再在server.js中連接。如果你可以從pg.connect中刪除連接,並嘗試使用index.js中的連接運行,它可能會阻止這個錯誤。