获取"应用程序错误"在heroku网站上使用postgres db

时间:2017-10-10 10:04:56

标签: node.js postgresql heroku

我已经用node.js和postgres编写了rest api的代码作为db。     它在我的系统中的邮递员运行正常,我已经将相同的代码部署到了heroku,它显示了#34;应用程序错误"在heroku网站。我有sequelize连接db与node.js

My code :


const pg = require('pg');
var Sequelize=require ('sequelize');
var app  = require('express')();         // Express App include
var http = require('http').Server(app); // http server
var env = app.get('env') == 'development' ? 'dev' : app.get('env');
var port = process.env.PORT || 8086;
var bodyParser = require("body-parser"); // Body parser for fetch posted data

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); // Body parser use JSON data

var sequelize = new Sequelize('testdb', 'postgres', 'Satya@9036', {
    host: 'localhost',
    port: 5432,
    dialect: 'postgres'
});
sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

  app.get('/Getdata',function(req,res){
    var data = {
        "Data":""
    };



    //GET method 
     sequelize.query("SELECT * FROM test_table ", { replacements: { visible: 'true' },type: sequelize.QueryTypes.SELECT})
  .then(function(test_table,err,rows) {
    // We don't need spread here, since only the results will be returned for select queries
    if(test_table)
    {
            data["Data"] = test_table;
            res.json(data);
        }
        else
        {
          data["Data"] = 'No data Found..';
          res.json(data);
        }

  });
});


When I run "heroku logs --tail --app boiling-river-40656 
" in terminal that shows following error.


**Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432**
2017-10-10T07:55:19.636702+00:00 app[web.1]:     at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:123:14)
2017-10-10T07:55:19.636703+00:00 app[web.1]:     at emitOne (events.js:96:13)
2017-10-10T07:55:19.636701+00:00 app[web.1]:     at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24)
2017-10-10T07:55:19.636703+00:00 app[web.1]:     at Connection.emit (events.js:188:7)
2017-10-10T07:55:19.636705+00:00 app[web.1]:     at emitOne (events.js:96:13)

请帮我解决这个问题。

先谢谢。

1 个答案:

答案 0 :(得分:0)

Log说你无法连接到Heroku上的127.0.0.1:5432。在127.0.0.1:5432没有postgres服务器。 你应该使用 heroku postgres附加组件(它有免费的计划名为" Hobby dev")。

严格建议您将 dev env与 heroku env分开,方法是声明环境变量并在代码中使用它。 数据库url,pwd,用户名应该转到env或设置文件。