我在Heroku测试应用程序中配置了PostgreSQL数据库。导航到URL / db不显示测试数据库,返回错误无法GET / db 。
Papertrail日志条目:
我已经包含了我为配置数据库而采取的步骤,并在下面列出了相关代码。
我已更新我的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'));
});