PostgreSQL 9.6.1 DB未在Heroku应用程序中显示

时间:2017-08-13 21:22:46

标签: node.js postgresql heroku

我在Heroku测试应用程序中配置了PostgreSQL数据库。导航到URL / db不显示测试数据库,返回错误无法GET / db

Papertrail日志条目:

https://papertrailapp.com/systems/glacial-bayou-96122/events?focus=833501717949202435&selected=833501717949202435

我已经包含了我为配置数据库而采取的步骤,并在下面列出了相关代码。

我已更新我的package.json文件以添加pg npm module

{
  "name": "node-js-getting-started",
  "version": "0.2.6",
  "description": "A sample Node.js app using Express 4",
  "engines": {
    "node": "6.11.1"
  },
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "node test.js"
  },
  "dependencies": {
    "ejs": "2.5.6",
    "express": "4.15.2" 
  },
  "dependencies": {
    "pg": "6.x",
    "ejs": "2.5.6",
    "express": "4.15.2",
    "cool-ascii-faces": "1.3.4"
  },
  "devDependencies": {
    "request": "^2.81.0",
    "tape": "^4.7.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/heroku/node-js-getting-started"
  },
  "keywords": [
    "node",
    "heroku",
    "express"
  ],
  "license": "MIT"
}

npminstall

并更新了我的index.js文件:

var express = require('express');
var app = express();
var pg = require('pg');

app.set('port', (process.env.PORT || 5000));

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

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.render('pages/index');
});

app.get('/cool', function(request, response) {
  response.send(cool());
});

app.get('/times', function(request, response) {
    var result = ''
    var times = process.env.TIMES || 5
    for (i=0; i < times; i++)
      result += i + ' ';
  response.send(result);
});

app.get('/db', function (request, response) {
  pg.connect(process.env.DATABASE_URL, function(err, client, done) {
    client.query('SELECT * FROM test_table', function(err, result) {
      done();
      if (err)
       { console.error(err); response.send("Error " + err); }
      else
       { response.render('pages/db', {results: result.rows} ); }
    });
  });
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

0 个答案:

没有答案