在尝试跟随Heroku开始使用node.js教程时,我到达了他们要求我定义配置变量和配置数据库的部分。当我按照提到的演练完成配置时,我尝试使用heroku open times
和heroku open db
打开它们,但所有我都是错误,指出Cannot GET /times
和Cannot GET /db
。
由于我是使用nodejs的新手,我不知道为什么应用程序返回此错误,我试图查找解决方案,但我找不到任何与我的问题密切相关的内容。
以下是我的index.js
const cool = require('cool-ascii-faces')
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const app = express();
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()))
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
app.get('/times', (req, res) => {
let result = ''
const times = process.env.TIMES || 5
for (i = 0; i < times; i++) {
result += i + ' '
}
res.send(result)
})
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: true
});
app.get('/db', async (req, res) => {
try {
const client = await pool.connect()
const result = await client.query('SELECT * FROM test_table');
res.render('pages/db', result);
client.release();
} catch (err) {
console.error(err);
res.send("Error " + err);
}
});
这是我的package.json
{
"name": "node-js-getting-started",
"version": "0.3.0",
"description": "A sample Node.js app using Express 4",
"engines": {
"node": "8.9.1"
},
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "node test.js"
},
"dependencies": {
"cool-ascii-faces": "^1.3.4",
"ejs": "^2.5.6",
"express": "^4.15.2",
"pg": "^7.4.1"
},
"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"
}
这是我的.gitignore
# Node build artifacts
node_modules
npm-debug.log
# Local development
*.env
*.dev
.DS_Store
# Docker
Dockerfile
docker-compose.yml
和我的procfile
web: node index.js
对于长篇文章,任何帮助都表示赞赏和抱歉。 :)
答案 0 :(得分:0)
这是我的index.js变体。实际上,您只需要使用Express 4重写建议的修改。
const cool = require('cool-ascii-faces')
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const pg = require('pg')
const config = {
connectionString: process.env.DATABASE_URL,
ssl: true
};
const pool = new pg.Pool(config)
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', function (req, res) {
let result = ''
const times = process.env.TIMES || 5
for (i = 0; i < times; i++) {
result += i + ' '
}
res.send(result)
})
.get('/db1', function (req, res, next) {
pool.connect(function (err, client, done) {
if (err) {
console.log("Can not connect to the DB" + err)
}
client.query('SELECT * FROM test_table;', function (err, result) {
done()
if (err) {
console.log(err)
}
res.status(200).send(result.rows)
})
})
})
.listen(PORT, () => console.log(`Listening on ${PORT}`))