我正在按照本教程在heroku上设置节点/快速应用:https://devcenter.heroku.com/articles/getting-started-with-nodejs#define-config-vars
所有步骤都在本地工作,但只有heroku open
和heroku open cool
有效。端点/ times和/ db给出无法获取。
heroku open times
//无法获取/次
heroku open db
//无法获取/ db
heroku open
//有效
heroku open cool
//有效
const cool = require('cool-ascii-faces')
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const { Pool } = require('pg')
const pool = new Pool({ connectionString: process.env.DATABASE_URL, ssl: true});
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/cool', (req, res) => res.send(cool()))
.get('/times', (req, res) => res.send(showTimes()))
.get('/db', async (req, res) => {
try {
const client = await pool.connect()
const result = await client.query('SELECT * FROM test_table');
const results = { 'results': (result) ? result.rows : null};
res.render('pages/db', results );
client.release();
} catch (err) {
console.error(err);
console.log(process.env.DATABASE_URL);
res.send("Error " + err);
}
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
showTimes = () => {
let result = ''
const times = process.env.TIMES || 5
for (i = 0; i < times; i++) {
result += i + ' '
}
return result;
}